Skip to content

Commit

Permalink
Added instant build
Browse files Browse the repository at this point in the history
  • Loading branch information
ash47 committed Dec 23, 2017
1 parent cc542af commit 0706b3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
52 changes: 36 additions & 16 deletions MethodInjector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ class Program

// A reference to the assembly we are hacking
private static Assembly theyAreBillionsAssembly;

// Original methods
private static MethodInfo originalEntity_EventOnUpdate = null;


// Called when the program is launched
static void Main(string[] args)
{
Expand Down Expand Up @@ -64,7 +61,9 @@ Perform patching
ReplaceMethod("ZX.ZXGame", "get_IsBetaPrivateVersion", BindingFlags.Static | BindingFlags.Public);

// Instant Build
//originalEntity_EventOnUpdate = ReplaceMethod("ZX.Components.CBuildable", "Entity_EventOnUpdate", BindingFlags.NonPublic | BindingFlags.Instance);
ReplaceMethod("ZX.ZXCommandDefaultParams", "get_BuildingTime", BindingFlags.Public | BindingFlags.Instance);
ReplaceMethod("ZX.ZXEntityDefaultParams", "get_BuildingTime", BindingFlags.Public | BindingFlags.Instance);


// Get a reference to the Main method
MethodInfo main = ZXProgram.GetMethod("Main", BindingFlags.Static | BindingFlags.NonPublic);
Expand All @@ -91,30 +90,51 @@ Perform patching

}

// Returns a field
public static FieldInfo GetField(string className, string fieldName, BindingFlags attr)
{
// Grab the method we will replace
Type theType = theyAreBillionsAssembly.GetType(className);
if (theType == null)
{
LogMessage("Failed to find class: " + className);
return null;
}

// Return the field
FieldInfo field = theType.GetField(fieldName, attr);

if(field == null)
{
LogMessage("Failed to find field: " + className + " :: " + fieldName);
}
return field;
}

// Replaces a method with one defined below
public static MethodInfo ReplaceMethod(string className, string methodName, BindingFlags attr)
public static void ReplaceMethod(string className, string methodName, BindingFlags attr)
{
// Grab the method we will replace
Type theType = theyAreBillionsAssembly.GetType(className);
if(theType == null)
{
LogMessage("Failed to find class: " + className);
return null;
return;
}

MethodInfo targetMethod = theType.GetMethod(methodName, attr);
if(targetMethod == null)
{
LogMessage("Failed to find method: " + className + " :: " + methodName);
return null;
return;
}

// Grab the method we will inject
MethodInfo newMethod = typeof(MethodInjector.Program).GetMethod(methodName, attr);
if (newMethod == null)
{
LogMessage("Failed to find replacement method: " + className + " :: " + methodName);
return null;
return;
}

// Prepare methods
Expand Down Expand Up @@ -148,6 +168,7 @@ public static MethodInfo ReplaceMethod(string className, string methodName, Bind

long* inj = (long*)newMethod.MethodHandle.Value.ToPointer() + 1;
long* tar = (long*)targetMethod.MethodHandle.Value.ToPointer() + 1;
long tarOriginal = *tar;
#if DEBUG
//Console.WriteLine("\nVersion x64 Debug\n");
byte* injInst = (byte*)*inj;
Expand All @@ -161,15 +182,13 @@ public static MethodInfo ReplaceMethod(string className, string methodName, Bind
#else
//Console.WriteLine("\nVersion x64 Release\n");
*tar = *inj;
*inj = tarOriginal;
#endif
}
}

// Log success
LogMessage("Successfully replaced: " + className + " :: " + methodName);

// Sucess
return targetMethod;
}

// Replacement for can pay resources
Expand Down Expand Up @@ -203,12 +222,13 @@ public static bool get_IsBetaPrivateVersion()
return true;
}

// Entity Update replacement
private void Entity_EventOnUpdate()
// The amount of time that things take to build
public int get_BuildingTime()
{

// Lowest possible build time
return 1;
}

private static void LogMessage(string error)
{
System.IO.File.AppendAllText("inject.log", "error: " + error + Environment.NewLine);
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Tools & Research notes for modding "They Are Billions"
- Allow loading of hacked save games -- This simply turns of the save game checksum code that is being used.
- Building costs no money -- This removes the cost of buildings, and allows you to build even if you have negative resources.
- Enable DEV and PRIVATE modes.
- Instant Build -- Makes buildings build almost instantly

### Cheats
- Enable cheats by pressing `control` + `shift` + `H`
Expand Down

0 comments on commit 0706b3f

Please sign in to comment.