Jump to content

Help with C#/.dll Zombie Spawning Code


schwanz9000

Recommended Posts

First off, would it be possible to get a C# or .DLL Modding Sub Forum? Good idea or not because of cheaters?

 

 

EDIT: Original question answered. THANKS HAL! Additional C# questions in the thread.

 

I'm writing another mod to dmustanger's Server Tools. This will add another forward slash command /spawn that will spawn zombies at (for now) hardcoded x, y, z coords inside our server's arena.

 

Everything about the code below works except for the 2 second wait time. Currently the command is sent, it waits 20 seconds, and then spawns all 10 zombies at once. I'd like to see it spawn a zombie every 2 seconds. I've tried Thread.Sleep and System.Threading.Timer and they both have the same outcome. I'm guessing it has something to do with my dll reporting back to the main game code before it actually spawns.

 

I'm sort of new to this, but I'm learning. :chuncky:

 

Any help is appreciated!

 

 

namespace ServerTools
{
   public class Arena
   {

       public static void StartSpawn()
       {
           int x = 1000;
           int y = 73;
           int z = 1000;
           string entityGroup = "ZombiesWasteland";

           long _chunkval = 0;
           int spawnCount = 0;

           for (spawnCount = 0; spawnCount < 10; spawnCount++)
           {
               int entityID = EntityGroups.GetRandomFromGroup(entityGroup);
               Entity spawnEntity = EntityFactory.CreateEntity(entityID, new Vector3((float)x, (float)y, (float)z));
               spawnEntity.SetSpawnerSource(EnumSpawnerSource.StaticSpawner, _chunkval, entityGroup);
               GameManager.Instance.World.SpawnEntityInWorld(spawnEntity);
               spawnEntity = null;
               Thread.Sleep(2000);
           }
       }

 

Link to comment
Share on other sites

First off, would it be possible to get a C# or .DLL Modding Sub Forum? Good idea or not because of cheaters?

 

I'm writing another mod to dmustanger's Server Tools. This will add another forward slash command /spawn that will spawn zombies at (for now) hardcoded x, y, z coords inside our server's arena.

 

Everything about the code below works except for the 2 second wait time. Currently the command is sent, it waits 20 seconds, and then spawns all 10 zombies at once. I'd like to see it spawn a zombie every 2 seconds. I've tried Thread.Sleep and System.Threading.Timer and they both have the same outcome. I'm guessing it has something to do with my dll reporting back to the main game code before it actually spawns.

 

I'm sort of new to this, but I'm learning. :chuncky:

 

Any help is appreciated!

 

 

namespace ServerTools
{
   public class Arena
   {

       public static void StartSpawn()
       {
           int x = 1000;
           int y = 73;
           int z = 1000;
           string entityGroup = "ZombiesWasteland";

           long _chunkval = 0;
           int spawnCount = 0;

           for (spawnCount = 0; spawnCount < 10; spawnCount++)
           {
               int entityID = EntityGroups.GetRandomFromGroup(entityGroup);
               Entity spawnEntity = EntityFactory.CreateEntity(entityID, new Vector3((float)x, (float)y, (float)z));
               spawnEntity.SetSpawnerSource(EnumSpawnerSource.StaticSpawner, _chunkval, entityGroup);
               GameManager.Instance.World.SpawnEntityInWorld(spawnEntity);
               spawnEntity = null;
               Thread.Sleep(2000);
           }
       }

 

Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs

Link to comment
Share on other sites

Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs

 

Thanks for the suggestion, but I want to avoid SDX mods and having to have my players download and install files.

Link to comment
Share on other sites

If you're running this code on the main thread then that would be expected behaviour. What you're doing is spawning a zombie, freezing the game for two seconds, then looping. So to the game it's like no time has passed at all and you'll get all the zombies at once. You said you're learning so here's rule one of game dev: never, ever, EVER pause the main loop ^^ If you did that in SP you'd just freeze the entire game for 20 seconds. You don't notice it on a dedi because your client isn't on that computer but the server is basically doing nothing for 20 seconds.

 

The easiest (not necessarily best) way is to use Unity's co-routine feature. Have a read up on them to see the pros and cons.

 

Change your spawn method to this:

 

 public static IEnumerator StartSpawn()
   {

       object wait = new WaitForSeconds(2);
       int x = 1000;
       int y = 73;
       int z = 1000;
       string entityGroup = "ZombiesWasteland";

       long _chunkval = 0;
       int spawnCount = 0;

       for (spawnCount = 0; spawnCount < 10; spawnCount++)
       {
           int entityID = EntityGroups.GetRandomFromGroup(entityGroup);
           Entity spawnEntity = EntityFactory.CreateEntity(entityID, new Vector3((float)x, (float)y, (float)z));
           spawnEntity.SetSpawnerSource(EnumSpawnerSource.StaticSpawner, _chunkval, entityGroup);
           GameManager.Instance.World.SpawnEntityInWorld(spawnEntity);
           spawnEntity = null;
           yield return wait;
       }

       yield return null;

   }

 

And then wherever you call the method, call it like this:

 

GameManager.Instance.StartCoroutine(Arena.StartSpawn());

Link to comment
Share on other sites

  • 2 months later...

+1

 

Besides that if you call ExecuteSync the command will definitely *always* be executed. That's why it's sync ;) (Make sure you only do this from the main thread though)

So in this case I'd assume that your code that makes this call is simply not executed itself.

Link to comment
Share on other sites

Why don't you use Mortelentus sdx spawner block. You place it as a block in your arena certain sections and it will spawn zs automatically. You can set it to how ever you like in the configs

 

You REALLY need to stop suggesting SDX as a solution for everything. Im a huge fan and user of it, but for general use on servers SDX is NOT cheap or in any way possible for a good majority of server renters.

Link to comment
Share on other sites

You REALLY need to stop suggesting SDX as a solution for everything. Im a huge fan and user of it, but for general use on servers SDX is NOT cheap or in any way possible for a good majority of server renters.

 

Ummm i was more suggesting the spawner. And how is it not cheap lol. And it was a complete different question anyways lol. But i was more saying also it could of assisted with what sdx can do it might be able to. Besides swanz was ok with me suggesting. He didnt want or need it and thats cool to.

 

And that reply was like 3 months ago lol. I will suggest what i like if it a good idea then cool of not well i offered help whether it was useful or not

Link to comment
Share on other sites

Have a look at the shutdown command class in a disassembler.

 

All it's doing is calling Application.Quit() so use that instead to turn the server off.

 

Once again, Hal to the rescue! I don't know why I didn't think about that in the first place. Doh!

 

What is your go to disassembler? I've used ILSpy. Does anyone have or been able to put together a clean deobfuscated Assembly-CSharp file? I haven't seen much about it since this.

 

+1

 

Besides that if you call ExecuteSync the command will definitely *always* be executed. That's why it's sync ;) (Make sure you only do this from the main thread though)

So in this case I'd assume that your code that makes this call is simply not executed itself.

 

I thought it wasn't working because cInfo was null. I'll have to check upstream calls. Works great if I'm logged onto the server though.

 

 

 

Basically trying to add my own customizable automatic server restart class. Blue Fang servers have a server side script that automatically restarts the server. This will be used to reboot the server once or twice a day on a set schedule.

 

When the server boots up, it starts a thread with a timer for x amount of time setup in the config file. If someone is online, it will give a warning message at the 10 minutes remaining mark and every minute there after. Then it will warn at the 30, 10, 5, 4, 3, 2, 1 second marks. If no one is online, I want it to shutdown and restart on it's own.

 

This is being added to my branch of dmustanger's ServerTools.

Link to comment
Share on other sites

Once again, Hal to the rescue! I don't know why I didn't think about that in the first place. Doh!

 

What is your go to disassembler? I've used ILSpy. Does anyone have or been able to put together a clean deobfuscated Assembly-CSharp file? I haven't seen much about it since this.

 

My favourite is DotPeek, it has a very handy search function on Control+T that halves the amount of time it takes me to track things down in the code. Very handy bit of kit.

 

If you want to de-obfuscate the DLL check out de4dot. That gave me the best results. I don't usually de-obfuscate these days as I've mainly grown accustomed to the source for most things but if I do want to see a cleaner version I run it through SDX. Dominix de-obfuscates the DLL and added a bit of code that adds all the string literals back in so you don't have to mess around converting all that A.O(234536) nonsense. Absolutely fantastic for increasing readability.

Link to comment
Share on other sites

  • 1 year later...
My favourite is DotPeek, it has a very handy search function on Control+T that halves the amount of time it takes me to track things down in the code. Very handy bit of kit.

 

If you want to de-obfuscate the DLL check out de4dot. That gave me the best results. I don't usually de-obfuscate these days as I've mainly grown accustomed to the source for most things but if I do want to see a cleaner version I run it through SDX. Dominix de-obfuscates the DLL and added a bit of code that adds all the string literals back in so you don't have to mess around converting all that A.O(234536) nonsense. Absolutely fantastic for increasing readability.

 

I don't care that this is old, doing a google search brought this up and to me that makes it relevant. especially since I don't think the info it has is out-dated.

 

Hal, I wanted to ask.

 

1) When you use de4dot on 7Days, do you apply any of the options de4dot has? or does it only need me to drag the dll onto the .exe?

2) Are you saying that merely installing SDX also cleans up the code? should I still do both de4dot and SDX?

3) What is Dominix?

Link to comment
Share on other sites

I don't care that this is old, doing a google search brought this up and to me that makes it relevant. especially since I don't think the info it has is out-dated.

 

Hal, I wanted to ask.

 

1) When you use de4dot on 7Days, do you apply any of the options de4dot has? or does it only need me to drag the dll onto the .exe?

2) Are you saying that merely installing SDX also cleans up the code? should I still do both de4dot and SDX?

3) What is Dominix?

 

A non-Hal answer:

 

SDX Launcher can run de4dot for you. Just do a SDX Build without having any selected mods, and it will clean it up for you.

 

Dominix is the original creator of SDX.

Link to comment
Share on other sites

I don't care that this is old, doing a google search brought this up and to me that makes it relevant. especially since I don't think the info it has is out-dated.

 

Hal, I wanted to ask.

 

1) When you use de4dot on 7Days, do you apply any of the options de4dot has? or does it only need me to drag the dll onto the .exe?

2) Are you saying that merely installing SDX also cleans up the code? should I still do both de4dot and SDX?

3) What is Dominix?

 

Yep, what SphereII said is spot on.

 

My new favourite for checking out the DLLs (once deobfuscated) is dnSpy. Control+Shift+K for the search all functionality and you can right click on classes to edit them directly.

Link to comment
Share on other sites

Ok, thank you both for clearing that up. Saved me alot of time, and cleared up some confusion I had about what was necessary to download and do.

 

Specifically, you guys helped with these points:

 

* I don't need ILSpy, de4Dot, .reflector, .reflix (or whatever those things are, i was gonna research them until just now...recent people on the Discord 7Days modding was saying I need them)

* I don't need to worry about de-obfuscation thanks to all the work put into SDX

*and the dilemma about if I should use dotPeek over dnSpy has been resolved.

 

so now I can get back to to meat and potatoes of learning how to mod this game.

Link to comment
Share on other sites

SDX Launcher can run de4dot for you. Just do a SDX Build without having any selected mods, and it will clean it up for you.

 

To clarify, I ran SDX with no mods on the working folder, and then opened the working .dll in dnSpy, and i guess this means things like this: https://i.imgur.com/HTKcaPa.png can't be removed? or does cleaned up mean other things?

 

Update: i musta messed something up because I got it working. The fake "for" loops and other fake things are gone.

Link to comment
Share on other sites

ya my bad i messed something up somehow, the fake "for" loops are gone now while using v7.1, but on both versions the weird "DZ" "ZZ" and other obscure names are still there. I submitted a more detailed report on your SDX Tutorial thread.

Link to comment
Share on other sites

ya my bad i messed something up somehow, the fake "for" loops are gone now while using v7.1, but on both versions the weird "DZ" "ZZ" and other obscure names are still there. I submitted a more detailed report on your SDX Tutorial thread.

 

Side effect from the de-obfuscated variables. They'll change with each release of the game. It's just something you need to work around and figure out what they do, based on variable type and context. As far as I know, there's no way to clean those up.

Link to comment
Share on other sites

Side effect from the de-obfuscated variables. They'll change with each release of the game. It's just something you need to work around and figure out what they do, based on variable type and context. As far as I know, there's no way to clean those up.

 

I noticed that if i use deobfuscator which attach with SDX - then it is not completely cleans the code (names) . If i use 64x latest version de4dot - all ok.

May be want to have to replace deobfuscator for SDX.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...