Skip to content

Instantly share code, notes, and snippets.

@pardeike
Last active November 7, 2021 09:57
Show Gist options
  • Save pardeike/a24c10abe76d92424f419d50fe16297e to your computer and use it in GitHub Desktop.
Save pardeike/a24c10abe76d92424f419d50fe16297e to your computer and use it in GitHub Desktop.
Customize Zombieland by creating a mod with CustomizeZombieland.ManipulateImage
using System.IO;
using UnityEngine;
using Verse;
namespace Test
{
public class ZombielandModifier : Mod
{
public ZombielandModifier(ModContentPack content) : base(content)
{
}
}
[StaticConstructorOnStartup]
public static class Tools
{
// Pumpkin.png in Textures with size 128x128
public static readonly Texture2D pumpkin = LoadTexture("Pumpkin");
static string GetModRootDirectory() => LoadedModManager.GetMod<ZombielandModifier>().Content.RootDir;
static Texture2D LoadTexture(string path)
{
var fullPath = Path.Combine(GetModRootDirectory(), "Textures", $"{path}.png");
var tex = new Texture2D(2, 2, TextureFormat.RGBA32, false, true);
_ = tex.LoadImage(File.ReadAllBytes(fullPath));
tex.Compress(true);
tex.wrapMode = TextureWrapMode.Clamp;
tex.filterMode = FilterMode.Trilinear;
tex.Apply(true, false);
return tex;
}
}
[StaticConstructorOnStartup]
public static class CustomizeZombieland // keep the name 'CustomizeZombieland' so ZL can find your code
{
/*
The arguments are almost self explaining:
- path is the string from https://github.com/pardeike/Zombieland/blob/master/Textures/Parts.cvs which corresponds to a tile in https://github.com/pardeike/Zombieland/blob/master/Textures/Parts.png
- Color[] is what you get from Textures2.GetPixels()
- width, height, rect correspond to the values from Parts.cvs
- setting noRecolor to true prevents Zombieland to apply zombie colors
*/
// keep the method exactly like this so ZL can find your code
public static void ManipulateImage(ref string path, ref int width, ref int height, ref Color[] pixels, ref Rect rect, ref bool noRecolor)
{
_ = width;
_ = height;
_ = rect;
if (path.StartsWith("Zombie/") == false) return;
if (path.StartsWith("Zombie/Naked")) return;
var pumpkinPixels = Tools.pumpkin.GetPixels();
if (pixels.Length != pumpkinPixels.Length) return;
pixels = pumpkinPixels;
noRecolor = true;
}
}
}
@newwby
Copy link

newwby commented Nov 6, 2021

As someone unfamiliar with RW modding when you say 'creating a mod with with CustomizeZombieland.ManipulateImage' do you mean as a separate dependent on Zombieland with the .cs file within mod/source, or as the packageID, or rather do you mean modifying Zombieland itself? I've been tinkering and I'm a little stuck/confused.

@pardeike
Copy link
Author

pardeike commented Nov 7, 2021

No, the trick here is to avoid a dependency. All it needs is to have your own mod and create that class with that method and Zombieland will detect it at runtime. It then makes a delegate out of it and calls it when needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment