Jump to content

30 posts in this topic Last Reply

Highlighted Posts

Posted:
Last Online:  
 

Thanks for taking the time! Hopefully.. some people will take this as a first step into the modding world! Thanks a lot!

Share this post


Link to post
Share on other sites
Posted:
Last Online:  
 

This is really cool, can't wait for the more functional parts! Many thanks for sharing your knowledge


ekCYJKD.pngTim The Terrible's Steam Workshop

Share this post


Link to post
Share on other sites
Posted:
Last Online:  
 

Thank you for adding the text editor method! It's great for those of us who use Linux and non-Windows OS :)

Share this post


Link to post
Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     
    8 hours ago, alborzka said:

    Thank you for adding the text editor method! It's great for those of us who use Linux and non-Windows OS :)

    Not really. You should take a look at MonoDevelop or Rider.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     
    On 2017-05-29 at 7:22 PM, boformer said:

    Not really. You should take a look at MonoDevelop or Rider.

    Hmm, I'm a physicist and whenever we code (usually in Python tbh) we just use a text editor (like Sublime) and then run the code in the terminal. I guess it's a different approach for C#, and that's why MonoDevelop or Rider are needed?

    7 hours ago, AnotherAdo said:

    Try Visual Studio Code.  https://code.visualstudio.com/

    No idea this existed, nice! Will definitely give this a go :)

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     
    5 hours ago, alborzka said:

    Hmm, I'm a physicist and whenever we code (usually in Python tbh) we just use a text editor (like Sublime) and then run the code in the terminal. I guess it's a different approach for C#, and that's why MonoDevelop or Rider are needed?

    No idea this existed, nice! Will definitely give this a go :)

    It doesn't really matter with what editor you write your code.

    What matters is that you are using a compiler that supports unsafe code. Unsafe code is required for detours. The compiler built into the game does not allow unsafe code. That means you need an external compiler.

    VS, Rider and MonoDevelop are coming with a compiler that supports external code, and it's very convenient. Alternatively, you could also use the command line to compile your .cs files.

    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Everything works fine if I let the game compile the code, but that exact same code compiled by VS 2017 always gives me an error. I even removed other mods installed but I get the same error (but if I compile that code with the game, that exact same code works fine!). I used the EXACT same VS settings you have here (I followed your instructions to a T, at least 10 times) and copied and pasted your code. I don't get it. I spent 6 hours googling and reading trying to figure out what could be wrong but no luck. Any help would be greatly appreciated.

    I get the following error when the game loads:

    A Mod caused an error [ModException]

    Details:
    System.Reflection.ReflectionTypeLoadException: The classes in the module cannot be loaded.
      at (wrapper managed-to-native) System.Reflection.Assembly:GetTypes (bool)
      at System.Reflection.Assembly.GetExportedTypes () [0x00000] in <filename unknown>:0
      at ColossalFramework.Plugins.PluginManager+PluginInfo.GetInstances[IUserMod] () [0x00000] in <filename unknown>:0
      at EntryData.GenerateData () [0x00000] in <filename unknown>:0

     

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Thank you for your reply.

    No, I followed the instructions very carefully and am familiar with dependencies etc. I have all the References added. I did exactly as the OP did... the exact program/code which is why this is so confusing.

    There is clearly something VS is putting in the compiled DLL that the game isn't and that the game doesn't like (maybe a setting in VS that I need to change?), but I can't figure out what that is. I've even loaded the DLL into a hex editor and I don't see anything strange with the class naming. The game appears to be complaining about not being able to load the classes within the DLL (yet it has no issues with those classes in the actual source file when compiling it).

    I'm wondering if the latest cities update may have changed something that it doesn't like that VS puts in the DLL (some sort of class declaration tag or something, since the game is complaining that a class cannot be loaded).

    Is there any way someone else can test the OP's program with the latest version of cities and the latest version of VS 2017?

    I'm hoping someone more familiar with the game than me has encountered and overcome with issue.

     

    Again, thank you for your reply.

     

    Edit: For the mod that I want to develop it cannot be done with the in-game compiler. So I need to get this working.

     

    Edit 2: I notice there must be some difference between the version of VS that the OP is using and I'm using because he can press F6 to build whereas mine does nothing, I have to press control->shift->B. I just did a fresh download and install of VS 2 days ago, so maybe the VS versions are different and one setting in the project is different, I don't know.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     
    On 5/27/2017 at 6:25 PM, boformer said:
    1. Paste the following in the Post-build event command line:
    
    if not exist "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\SecondMod" mkdir "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\SecondMod"
    del /q /f "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\SecondMod\"
    xcopy /y "C:\Users\Felix\Desktop\SecondMod\SecondMod\bin\Debug\netstandard1.4\SecondMod.dll" "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\SecondMod"

    In the third line, replace the path C:\Users\Felix\Desktop\SecondMod\ with your project path.

    You can use Visual Studio substitutions like $(TargetPath) in the post-build event so you won't have to customize it. Here's the one I've been using:

    mkdir "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)"
    del "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)\$(TargetFileName)"
    xcopy /y "$(TargetPath)" "%LOCALAPPDATA%\Colossal Order\Cities_Skylines\Addons\Mods\$(SolutionName)"

     

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     
    4 hours ago, imabzb said:

    No, I followed the instructions very carefully and am familiar with dependencies etc. I have all the References added. I did exactly as the OP did... the exact program/code which is why this is so confusing.

    There is clearly something VS is putting in the compiled DLL that the game isn't and that the game doesn't like (maybe a setting in VS that I need to change?), but I can't figure out what that is. I've even loaded the DLL into a hex editor and I don't see anything strange with the class naming. The game appears to be complaining about not being able to load the classes within the DLL (yet it has no issues with those classes in the actual source file when compiling it).

    Double check the target .NET Framework version, on the Application tab of the project properties. It should be set to .NET Framework 3.5. You can also check the Runtime Version property of all your references (even the ones that were added automatically), which should say v2.0.50727.

    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Hi TaradinoC

     

    Thanks for your reply.

    1. I'm not sure what version of VS you're using, but the macro names have been broken for some time now as has been reported often in the VS community (I've tried them many times with no luck). The macros report incorrect file paths. If you update VS to 2017 latest version, I suggest you either use $(MSBuildProjectDirectory) or $(MSBuildThisFileDirectory) or better yet, edit the csproj file directly and enter your own target with something like this near the end of the file:

     

    <Target Name="MyAfterBuild" AfterTargets="Build">
      <Exec Command="copyFiles.bat" />
    </Target>

    The latter worked great for me.

     

    2. I made certain that the project properties were set to .NET 3.5 when I created the project as instructed by the OP.

     

    The SDK->NETStandard.Library files in dependencies all read 4.3 regardless of what I select in the project.

     

    On the Project Applications tab I have Target Framework: .NETStandard 1.4 (same as OP). It only goes up to 1.6 (and I've tried them all!), but says you can download others, which I did, but they still never show up in the list. I suspected this may be the problem, but selecting "Install other frameworks" did not give me the option to actually select one (I typically use C++, Jass/jc, and asm for development, so I'm not sure if I have everything installed that is required for C#).

     

    Is there an option somewhere to add all the .NET components? Or maybe I'm missing something more specific like a subset.

     

    Thanks

     

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     
    28 minutes ago, imabzb said:

    I'm not sure what version of VS you're using, but the macro names have been broken for some time now as has been reported often in the VS community (I've tried them many times with no luck). The macros report incorrect file paths.

    Hmm, I haven't had that problem, and I'm using VS 2017...

    After searching for it, I see people reporting the problem with .NET Core/Standard specifically. And I just noticed that the OP says to make a .NET Standard project instead of .NET Framework.

    Have you tried using the Class Library (.NET Framework) project template instead? It's under Windows Classic Desktop.

    I haven't used .NET Core/Standard myself, and I don't know why it'd work for OP but not for you, but it looks like Standard 1.0 corresponds to a minimum Framework version of 4.5, so that might explain the conflict.


      Edited by TaradinoC  

    link/clarification for macro problem reports
    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    ah... well, I did install and use what OP said ".NET Standard". .NET framework adds a lot more files and adds a bunch of "using" statements, which I left in, assuming each file requires them?

    At least now I see .NET 3.5 in my project properties!

    In any event, I created a new project with .NET Framework and now I get a new error :(

     

    A Mod caused an error [ModException]

    Details:
    System.IndexOutOfRangeException: Array index is out of range.
      at EntryData.GenerateData () [0x00000] in <filename unknown>:0

     

    I wish the game would be a little bit more informative.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Thank you, TaradinoC! You are a God send!

     

    That actually worked. I definitely have to use .NET Framework, not .NET Standard.

    The reason for the exception was that when I created a new project I forgot to add : IUserMod lol (I've only be working with C# for a day, but I'll get there lol)

     

    Problem solved. Thanks so much. You have no idea how crazy this was driving me for the last 24 hours.

     

    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    You were also right about the macros, they seem to work fine with .NET Framework but not with .NET Standard. Strange that this would be the case. But again, thanks for the info :)

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     
    1 hour ago, imabzb said:

    You were also right about the macros, they seem to work fine with .NET Framework but not with .NET Standard. Strange that this would be the case. But again, thanks for the info :)

    Nice catch. I'm usually using VS2015, but switched to 2017 for this tutorial. The macros with placeholders stopped working in VS2017.

    I will edit the tutorial and add this information when I have time.

    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     
    On 6/4/2017 at 4:38 AM, imabzb said:

    That actually worked. I definitely have to use .NET Framework, not .NET Standard.

    I'm in the same boat. I attempted to create a project using .NET Standard instead of .NET Framework, and received the same error. While using the .NET Framework template and CTRL-C everything from my old project, I was able to successfully load the mod to CS.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Thanks for the tutorial! I can hardly wait the 10 days before Spring Break (because I need to study for my exams and wait to start diving into code).


    Ocram's Razor: Though "more things shouldn't be used than are necessary," they're just too fun to pass up! Expect many verbose arguments from me. I will try to write abstracts before or short summaries after from now on.

    Words to live by:
    "Now there are varieties of gifts, but the same Spirit. But to each one is given the manifestation of the Spirit for the common good. For to one is given the word of wisdom through the Spirit, and to another the word of knowledge according to the same Spirit; to another faith by the same Spirit, and to another gifts of healing by the one Spirit... But one and the same Spirit works all these things, distributing to each one individually..." 1 Corinthians 4-11

    "Do not worry about tomorrow; for tomorrow will care for itself. Each day has enough trouble of its own." Matthew 6:34
    "Do not judge so that you will not be judged. For in the way you judge, you will be judged; and by your standard of measure, it will be measured to you." Matthew 7:1-3

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     

    I edited the top post. The "classic" .NET Framework is the better choice for this mod.

    • Like 1

    Share this post


    Link to post
    Share on other sites
  • Original Poster
  • Posted:
    Last Online:  
     

    I just published the second tutorial:

     

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Also note that if you are working on the Mac and need to locate the assemblies to reference, they are located in the steam folder 
    /Users/[UserName]/Library/Application Support/Steam/steamapps/common/Cities_Skylines . In this folder there is a Cities.app file. This is a package file. Right click on the file and choose View Package Contents. This will expand the package. The assemblies (.dll) are in the /Contents/Resources/Data/Managed folder.

    And the folder to publish your mod on the mac is /Users/[UserName]//Library/Application Support/Colossal Order/Cities_Skylines/Addons/Mods

    • Like 1

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Hi, thanks a lot for your tutorial. Things may have changed with the newest VS, because I add dependencies, it says that the dll files are not strong-named. Do I have to go and sign all of these dll files manually?

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Nevermind, the files were somehow altered and I had to verify their integrity in steam. Then it worked. Same thing happened with workshop dlls. I had to unsub and resub for them to work.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Heya, does this still work with .NET framework 3.1 or .NET 5.0, using VS 2019? (In other words, how vital is it that one uses .NET Framework 3.5 exactly?)

     

    I'm having a strange issue where I follow the instructions (as best I can) for Visual Studio 2019, however the compiled Mod doesn't show the supplied name or description in the Content Manager. Putting the .cs file in the location from the first step works fine("SecondSecondMod" in the image), so I'm wondering if it's the framework I'm using?

    //Happy to spin this into it's own thread if modern dev setups are a separate topic :"D

    studio19.jpg

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    After getting some pointers on the steam forums, I found that as of Visual Studio 2019, you can't change Framework type through the settings UI.

    Here's how to setup a new project in Visual Studio 2019 for development of CS Mods:

    1. Create a New Project (or take an existing project and skip this step):

      • C# Class Library (language "C#", project type "Library")

      • Project/Solution name and saved location as desired (refer original post)

      • Choose any Target Framework, we'll be changing this in the following steps.

    2. Once created, Right click the name of the project in Solution Explorer (eg SecondMod), and select Unload Project

      • This opens the .csproj file for editing.

    3. Under <PropertyGroup>, Change <TargetFramework> and add <LangVersion> to match the following:

      <TargetFramework>net35</TargetFramework>
      <LangVersion>preview</LangVersion>

      This gets you off the 'Core' or 'Standard' frameworks and onto the "Framework n.n" frameworks. LangVersion is there to force the program to allow you to use a more current version of C#, which otherwise leads to compilation errors.
      You may not need LangVersion to compile your code, your mileage will vary on this.

    4. Save

    5. Right click project ( eg "SecondMod (Unloaded)" in Solution Explorer > Reload Project )

    Now if you right click the solution name and select properties, the Target Frameworks should read ".NET Framework 3.5". Other, more recent versions may be available, if you want to use a newer version that should be fine, the point is you can now select these from the UI!

    From this point, the remainder of the tutorial remains the same (or close enough) in Visual Studio 2019. Hopefully 2022 doesn't change much beyond this! :)

    --

    This is probably bread and butter .NET, but in my defense the last time I used C#, we were still using Visual Studio 2012!


      Edited by icy mccoolname  

    Added step-by-step instructions for a new project in VS2019

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    I will confirm that the above steps do work in Visual Studio 2022 Community Edition.

    Share this post


    Link to post
    Share on other sites
    Posted:
    Last Online:  
     

    Doing this causes an IndexOutOfRangeException in OptionsMainPanel.AddUserMods. I literally just have an empty class like in the OP, and enabling it in the mod menu causes an error popup.

    I even tried removing that class, so I just have an empty dll build, and it still gives me the same error!

     

    Edit: Nevermind, it was a linking error.

     

    image.png.11fcd41b302920b93cd66f405bd9719f.png

    Share this post


    Link to post
    Share on other sites

    Sign In or register to comment...

    To comment in reply, you must be a community member

    Sign In  

    Already have an account? Sign in here.

    Sign In Now

    Create an Account  

    Sign up to join our friendly community. It's easy!  

    Register a New Account


    ×

    Thank You for the Continued Support!

    Simtropolis depends on donations to fund site maintenance costs.
    Without your support, we just would not be in our 24th year online!  You really help make this a great community. *:thumb:

    But we still need your support to stay online. If you're able to, please consider a donation to help us stay up and running. This helps sustain a platform where we can share our community creations for years to come.

    Make a Donation, Get a Gift!

    Expand your city with the best from the Simtropolis Exchange.
    Make a Donation and get one or all three discs today!

    STEX Collections

    By way of a "Thank You" gift, we'd like to send you our STEX Collector's DVD. It's some of the best buildings, lots, maps and mods collected for you over the years. Check out the STEX Collections for more info.

    Each donation helps keep Simtropolis online, open and free!

    Thank you for reading and enjoy the site!

    More About STEX Collections