Skip to content

Commit

Permalink
Add project
Browse files Browse the repository at this point in the history
  • Loading branch information
Lustyn committed Jan 28, 2018
1 parent d378da2 commit ef536f3
Show file tree
Hide file tree
Showing 10 changed files with 635 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ bld/
[Bb]in/
[Oo]bj/
[Ll]og/
[Ii]nstall/

# Visual Studio 2015 cache/options directory
.vs/
Expand Down
29 changes: 29 additions & 0 deletions ShopSearch.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio 15
VisualStudioVersion = 15.0.27004.2009
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ShopSearch", "ShopSearch\ShopSearch.csproj", "{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Debug|Any CPU.ActiveCfg = Debug|x86
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Debug|x86.ActiveCfg = Debug|x86
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Debug|x86.Build.0 = Debug|x86
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Release|Any CPU.ActiveCfg = Release|x86
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Release|x86.ActiveCfg = Release|x86
{B9FF5797-1C40-459A-97D2-C3C68E49A2FB}.Release|x86.Build.0 = Release|x86
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {5B75A1EF-E26B-4DDF-9837-65F03763E071}
EndGlobalSection
EndGlobal
Binary file added ShopSearch/Assemblies/0Harmony.dll
Binary file not shown.
127 changes: 127 additions & 0 deletions ShopSearch/EmbeddedAssembly.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Reflection;
using System.Security.Cryptography;

/// <summary>
/// A class for loading Embedded Assembly
/// </summary>
public class EmbeddedAssembly
{
// Version 1.3

static Dictionary<string, Assembly> dic = null;

/// <summary>
/// Load Assembly, DLL from Embedded Resources into memory.
/// </summary>
/// <param name="embeddedResource">Embedded Resource string. Example: WindowsFormsApplication1.SomeTools.dll</param>
/// <param name="fileName">File Name. Example: SomeTools.dll</param>
public static void Load(string embeddedResource, string fileName)
{
if (dic == null)
dic = new Dictionary<string, Assembly>();

byte[] ba = null;
Assembly asm = null;
Assembly curAsm = Assembly.GetExecutingAssembly();

using (Stream stm = curAsm.GetManifestResourceStream(embeddedResource))
{
// Either the file is not existed or it is not mark as embedded resource
if (stm == null)
throw new Exception(embeddedResource + " is not found in Embedded Resources.");

// Get byte[] from the file from embedded resource
ba = new byte[(int)stm.Length];
stm.Read(ba, 0, (int)stm.Length);
try
{
asm = Assembly.Load(ba);

// Add the assembly/dll into dictionary
dic.Add(asm.FullName, asm);
return;
}
catch
{
// Purposely do nothing
// Unmanaged dll or assembly cannot be loaded directly from byte[]
// Let the process fall through for next part
}
}

bool fileOk = false;
string tempFile = "";

using (SHA1CryptoServiceProvider sha1 = new SHA1CryptoServiceProvider())
{
// Get the hash value from embedded DLL/assembly
string fileHash = BitConverter.ToString(sha1.ComputeHash(ba)).Replace("-", string.Empty);

// Define the temporary storage location of the DLL/assembly
tempFile = Path.GetTempPath() + fileName;

// Determines whether the DLL/assembly is existed or not
if (File.Exists(tempFile))
{
// Get the hash value of the existed file
byte[] bb = File.ReadAllBytes(tempFile);
string fileHash2 = BitConverter.ToString(sha1.ComputeHash(bb)).Replace("-", string.Empty);

// Compare the existed DLL/assembly with the Embedded DLL/assembly
if (fileHash == fileHash2)
{
// Same file
fileOk = true;
}
else
{
// Not same
fileOk = false;
}
}
else
{
// The DLL/assembly is not existed yet
fileOk = false;
}
}

// Create the file on disk
if (!fileOk)
{
System.IO.File.WriteAllBytes(tempFile, ba);
}

// Load it into memory
asm = Assembly.LoadFile(tempFile);

// Add the loaded DLL/assembly into dictionary
if(!dic.ContainsKey(asm.FullName))
dic.Add(asm.FullName, asm);
}

/// <summary>
/// Retrieve specific loaded DLL/assembly from memory
/// </summary>
/// <param name="assemblyFullName"></param>
/// <returns></returns>
public static Assembly Get(string assemblyFullName)
{
if (dic == null || dic.Count == 0)
return null;

if (dic.ContainsKey(assemblyFullName))
return dic[assemblyFullName];

return null;

// Don't throw Exception if the dictionary does not contain the requested assembly.
// This is because the event of AssemblyResolve will be raised for every
// Embedded Resources (such as pictures) of the projects.
// Those resources wil not be loaded by this class and will not exist in dictionary.
}
}
22 changes: 22 additions & 0 deletions ShopSearch/Logger.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ItemSearch
{
enum LogType
{
ERROR,
INFO
}

static class Logger
{
public delegate void LogEvent(LogType type, string msg);
public static event LogEvent Logged;

public static void Info(string msg) => Logged(LogType.INFO, msg);
public static void Error(string msg) => Logged(LogType.ERROR, msg);
}
}
143 changes: 143 additions & 0 deletions ShopSearch/Patch/ShopMenuPatch.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
using Harmony;
using Microsoft.Xna.Framework;
using StudioForge.Engine.GUI;
using StudioForge.Engine.Integration;
using StudioForge.TotalMiner;
using StudioForge.TotalMiner.API;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace ItemSearch.Patch
{
[HarmonyPatch]
public class ShopMenuPatch
{
public const string TypeName = "StudioForge.TotalMiner.Screens2.ShopMenu";
public const string MethodName = "InitMainContainer";

static NewGuiMenu shopMenuInstance;
struct SearchMenuData
{
public string SearchText;
}
static SearchMenuData data;

[HarmonyPostfix]
static void Postfix(object __instance)
{
try
{
if (data.SearchText == null)
data.SearchText = "";
var menu = __instance as NewGuiMenu;
if (menu == null)
return;
shopMenuInstance = menu;

Canvas canvas = shopMenuInstance.canvas;
Window win;
TextBox tbox;
DataField df;

int g = 4;
var shopInventoryText = canvas.FindChild("Shop Inventory");
var shopTraverse = Traverse.Create(__instance);
var invTabPane = shopTraverse.Field("tabsPane");
var mainWin = invTabPane.Field("mainWin").GetValue<Window>();
var itemsTab = mainWin.FindChild("itemsTab");
var itemx = (int)itemsTab.Position.X;
var itemy = (int)itemsTab.Position.Y;
int x = itemx + itemsTab.Size.X + g;
int y = itemy;
int w = shopInventoryText.Size.X - itemsTab.Size.X * 2 - g;
int h = itemsTab.Size.Y;
float scale = 0.6f;

win = tbox = new TextBox("Search:", x, y, w / 4, h, scale)
{
Name = "searchLabel"
};
win.Colors = Colors.LabelLowAlphaColors;
mainWin.AddChild(win);
win = tbox = df = new DataField(data.SearchText, x + (w / 4) + 1, y, w / 4 * 3, h, scale)
{
Name = "searchField",
TextAlignX = WinTextAlignX.Left
};
((ITextInputWindow)df).OnValidateInput = TextChanged;
mainWin.AddChild(win);
} catch (Exception e)
{
Logger.Error(e.ToString());
}
}

[HarmonyTargetMethod]
static MethodInfo CalculateMethod(HarmonyInstance harmony)
{
return AccessTools.Method(AccessTools.TypeByName(TypeName), MethodName);
}

static void TextChanged(ITextInputWindow win)
{
data.SearchText = win.Text;
PopulateItems(data.SearchText);
}

static void PopulateItems(string name)
{
var shopMenuTraverse = Traverse.Create(shopMenuInstance);
var invTabPaneTraverse = shopMenuTraverse.Field("tabsPane");
var mainWin = invTabPaneTraverse.Field("mainWin").GetValue<Window>();
var itemsTab = mainWin.FindChild("itemsTab");
var blocksTab = mainWin.FindChild("blocksTab");
var tabHighLight = invTabPaneTraverse.Field("tabHighLight").GetValue<Window>();
var invPaneTraverse = invTabPaneTraverse.Field("invPane");
var inv = invTabPaneTraverse.Field("inventory").GetValue<ITMInventory>();
var slotID = invTabPaneTraverse.Field("slotID");
slotID.SetValue(0);
invTabPaneTraverse.Field("currentPage").SetValue(0);
invTabPaneTraverse.Field("pageCountWin").Property("Text").SetValue("1");
invTabPaneTraverse.Field("morePages").SetValue(false);
invTabPaneTraverse.Field("pageCountContainerWin").GetValue<Window>().IsVisible = false;
inv.Clear();
ItemInvType invType;
int startIndex;
if((tabHighLight.Parent as Window).Name == "itemsTab")
{
startIndex = Globals1.BlockData.Length;
invType = (ItemInvType)((byte)(invTabPaneTraverse.Field("itemsTabID").GetValue<int>() + 8));
} else
{
startIndex = 0;
invType = (ItemInvType)((byte)(invTabPaneTraverse.Field("blocksTabID").GetValue<int>() + 1));
}
for (int i = startIndex; i < (int)Globals1.ItemData.Length && slotID.GetValue<int>() < inv.PackSize; i++)
{
ItemDataXML itemData = Globals1.ItemData[i];
if (PopulateItem(itemData) && Globals1.ItemTypeData[i].Inv == invType)
{
if (data.SearchText == "" || itemData.Name.ToLowerInvariant().Contains(data.SearchText.ToLowerInvariant()) || itemData.Name.ToLowerInvariant() == data.SearchText.ToLowerInvariant())
{
InventoryItem inventoryItem = new InventoryItem(itemData.ItemID, 1);
slotID.SetValue(slotID.GetValue<int>() + 1);
inv.Items.Add(inventoryItem);
}
}
}
invPaneTraverse.Method("RefreshInventoryWindowItems").GetValue();
}

static bool PopulateItem(ItemDataXML data)
{
if(data.IsValid && data.IsEnabled && !data.HasItemProxy && data.MinCSPrice > 0 && data.ItemID != Item.MobSpawn)
{
return true;
}
return false;
}
}
}
34 changes: 34 additions & 0 deletions ShopSearch/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ItemSearch")]
[assembly: AssemblyProduct("ItemSearch")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyCopyright("Copyright © 2018")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type. Only Windows
// assemblies support COM.
[assembly: ComVisible(false)]

// On Windows, the following GUID is for the ID of the typelib if this
// project is exposed to COM. On other platforms, it unique identifies the
// title storage container when deploying this assembly to the device.
[assembly: Guid("b9ff5797-1c40-459a-97d2-c3c68e49a2fb")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
[assembly: AssemblyVersion("1.0.0.0")]
Loading

0 comments on commit ef536f3

Please sign in to comment.