Fractal Softworks Forum

Please login or register.

Login with username, password and session length
Advanced search  

News:

Starsector 0.97a is out! (02/02/24); New blog post: Anubis-class Cruiser (12/20/24)

Author Topic: VSCode setup for Starsector modding (2024)  (Read 2201 times)

Anexgohan

  • Lieutenant
  • **
  • Posts: 80
  • Anexweapons
    • View Profile
VSCode setup for Starsector modding (2024)
« on: March 28, 2024, 02:01:02 AM »

This guide will help you set up Visual Studio Code for Starsector modding.
It will cover setting up the IDE, installing the required extensions, and configuring the build tasks.

Steps:

(A).
Setting up Java 1.7 (you can skip this step if you already have Java 1.7 installed) =>
Note: you can use any java version from 1.7, 1.8, till java 17, but 1.7 is recommended for Starsector modding.

1. Install Java 1.7 from [Java 1.7]

Link Provided by the [Starsector Wiki]


2. Extract the downloaded JDK7.zip to a folder.
        For example the default java directory,
       
Code
C:\Program Files\Java\JDK7
.

(B).
Installing Visual Studio Code =>

1. Install Visual Studio Code from [VSCODE]


2. Open Visual Studio Code. Go to the Extensions tab on the left sidebar and install the following extensions:
    - Extension Pack for Java by Microsoft (Java Extension Pack) - This extension pack includes the following extensions:
        * Language Support for Java(TM) by Red Hat
        * Debugger for Java
        * Test Runner for Java
        * Maven for Java
        * Project Manager for Java
        * Visual Studio IntelliCode (optional/not required/safe to uninstall)


3. Press `Ctrl + Shift + P` to open the command palette and type `Java: Create Java Project`.
        Select `Create Java Project` from the list.


4. Select `No build tools` and press Enter.

5. Select the mods folder inside the Starscetor installation folder as the project folder.
        The path should look like this:
       
Code
Starsector\mods

6. Enter the project name. The project name should be the name of the mod you are creating.
        For example, if you are creating a mod called `MyMod`,  enter `MyMod` as the project name.
        The folder structure should end up looking like this:
       
Code
Starsector\mods\MyMod
7. This will create the following files and folders inside the `MyMod` folder. This will start you up with a basic mod structure.
        - .vscode
            - settings.json
        - lib
        - src
        - README.md
Note:
settings.json - This contains the configuration related to VSCode within the project
src - You will be writing your code in this folder, this is where the mod's source code goes,
        anyone with this folder and the files contained inside it should be able to compile the mod


8. Open the `MyMod` folder in Visual Studio Code. File -> Open Folder -> Select the `MyMod` folder. This should load the project in VSCode.

9. In VSCode click on 'settings.json' in the .vscode folder.
        Delete everything inside settings.json, Copy and Paste the following configuration to the settings.json file:
Code
{
    "java.project.sourcePaths": ["src"],
    "java.project.outputPath": "out\\vscode\\bin",
    "java.project.referencedLibraries": [
        "lib/**/*.jar",
        "d:\\Starsector\\starsector-core\\starfarer.api.jar",
        "d:\\Starsector\\starsector-core\\lwjgl.jar",
        "d:\\Starsector\\starsector-core\\lwjgl_util.jar",
        "d:\\Starsector\\starsector-core\\xstream-1.4.10.jar",
        "d:\\Starsector\\mods\\LazyLib\\jars\\LazyLib.jar",
        "d:\\Starsector\\mods\\MagicLib\\jars\\MagicLib.jar",
        "d:\\Starsector\\mods\\zz GraphicsLib\\jars\\Graphics.jar"
    ],
    "java.configuration.runtimes": [
        {
            "name":"JavaSE-1.7",
            "path":"C:\\Program Files\\Java\\JDK7",
            "default": true
        }
    ]
}
Note: this assumes that you have installed Starsector in the `D:\Starsector` directory.
        If you have installed Starsector in a different directory,
        you will need to update the paths in the `java.project.referencedLibraries` section.


You can add more libraries by also clicking on the `Add` button next to "Refrenced Libraries" tab


10. Save the settings.json file and restart VSCode and load the 'MyMod' project again.

(C).
work on the mod =>


1. Start coding your mod in the `src` folder. Once you click any .java file in the `src` folder,
        A new drop-down will appear at the left panel of the VSCode window called
        ```Java Projects```.
        Click on the `MyMod` project to see the project structure.
      

2. Change the output folder of the compiled .jar file
        - Goto File -> Preferences -> Settings
        - Search for `Export Jar: Target Path`
        Change:
       
Code
${workspaceFolder}/${workspaceFolderBasename}.jar
        to
       
Code
${workspaceFolder}/jars/${workspaceFolderBasename}.jar
        in either 'User' or 'Workspace' settings
        you can also change the '${workspaceFolderBasename}' to anything you want. e.g.
       
Code
${workspaceFolder}/jars/mymod.jar
      
      
To Build the MyMod.jar file =>
3. click the `Run Build Task` button '->' at the bottom of the window, next to `Java Projects`

Congratulations you have successfully set up Visual Studio Code for Starsector modding.
Logged

Anexgohan

  • Lieutenant
  • **
  • Posts: 80
  • Anexweapons
    • View Profile
Re: VSCode setup for Starsector modding (2024)
« Reply #1 on: March 28, 2024, 02:01:27 AM »

reserved.
Logged

Anexgohan

  • Lieutenant
  • **
  • Posts: 80
  • Anexweapons
    • View Profile
Re: VSCode setup for Starsector modding (2024)
« Reply #2 on: April 28, 2024, 03:29:59 AM »

Bonus:
Create a local latest.zip of your mod on windows
If you are publishing your mod I would suggest github actions instead to auto-create latest.zip upon push.

Make a file in your mod folder called "make-latest-zip.bat"
paste the following in it:
Spoiler
Code
@echo off
setlocal enabledelayedexpansion

::     "version": { "major":"0", "minor": "2", "patch": "9f" },, "minor": "2", "patch": "9f" },
:: Set the input and 7zip directories
set "sevenZip=C:\Program Files\7-Zip\7z.exe"
set "sourceDir=D:\Games\Space-4x\Starsector-mod-dev\mods\Explore the Galaxy"
set "modsDirectory=D:\Games\Space-4x\Starsector-mod-dev\mods\Explore the Galaxy\output"

:: get the name of the mod from the sourceDir
:: for %%i in ("!sourceDir!") do set "modName=%%~nxi"

set "mod_info-filePath=!sourceDir!\mod_info.json"
:: if the above file is not fount prompt user and exit the script with any key
if not exist "%mod_info-filePath%" (
    echo File not found: %mod_info-filePath%
    pause
    exit /b
)

:: there are multiple lines in mod_info-filePath with the format "name": "*.*", save the first line to a variable
for /f "delims=" %%i in ('findstr /C:"name" "%mod_info-filePath%"') do (
    set "nameLine=%%i"
    goto :break
)
:break
:: Extract everything after ["name":] and save it to a variable "modFolderName"
for /f "tokens=2 delims=:," %%i in ("!nameLine!") do set "modFolderName=%%i"
:: remove the quotes from the variable
set "modFolderName=!modFolderName:"=!"

REM set "modFolderName=!modFolderName:~1,-1!"

:: remove the first and last space from the variable, do not delete the spaces in the middle or any characters
for /f "tokens=*" %%i in ("!modFolderName!") do set "modFolderName=%%i"

:: echo Mod folder name:!modFolderName!

:: Search for the string and save it to a variable
for /f "delims=" %%i in ('findstr /C:"version" "%mod_info-filePath%"') do set "versionLine=%%i"
:: Display the result
:: echo !versionLine!

:: Extract the version number from the string and save it to separate variables
:: tokens define the parts of the string to extract
for /f "tokens=4,6,8 delims=:, " %%i in ("!versionLine!") do (
    set "major=%%i"
    set "minor=%%j"
    set "patch=%%k"
)
:: remove the quotes from the variables
set "vMajor=!major:"=!"
set "vMinor=!minor:"=!"
set "vPatch=!patch:"=!"
:: set a variable to the version number with the format "Explore the Galaxy-major.minor.patch"
set "versionFormat=!modFolderName!-!vMajor!.!vMinor!.!vPatch!"
:: display mod info taken from the mod_info-filePath file
echo.
echo    Mod Name: "!modFolderName!"
echo    Version:
echo    Major: "!vMajor!" Minor: "!vMinor!" Patch: "!vPatch!"
echo    Mod Folder will be created as: "!versionFormat!"

:: Set the output and extract directories using the modName
set "outputDir=%modsDirectory%\zip\!versionFormat!.zip"
:: set outputDirVar to the outputDir without the !versionFormat!.zip
set "outputDirVar=%modsDirectory%\zip"
set "extractDir=%modsDirectory%\!versionFormat!"


:: print multiple lines of text to the console\
echo _______________________________________________________________
echo    Source Directory:
echo    "%sourceDir%"
echo _______________________________________________________________
echo.
echo    This will create the zip file in a temporary folder:
echo    "%outputDir%\%versionFormat%.zip"
echo    this temporary folder will be deleted after extraction
echo _______________________________________________________________
echo.
echo    zip file will be extracted to the mods folder:
echo    "%extractDir% "
echo    with the folder name "%versionFormat%"
echo _______________________________________________________________
echo.
echo    any folder with the name : "%versionFormat%" or matching "!modFolderName!-*.*.*" will be deleted
echo _______________________________________________________________
echo.
set /p userInput=" Press Enter to continue..."

:: delete the extractDir if it exists
if exist "%extractDir%" rd /s /q "%extractDir%"
:: delete any folder in "modsDirectory" that matches "modFolderName-*.*.*"
for /d %%i in ("%modsDirectory%\!modFolderName!*") do rd /s /q "%%i"

:: use the 7zip command to create a zip file from the source directory, files and folders are defined below:
"%sevenZip%" a -tzip -bb1 "%outputDir%" "%sourceDir%\data" "%sourceDir%\graphics" "%sourceDir%\jars" "%sourceDir%\sounds" "%sourceDir%\src" "%sourceDir%\mod_pictures" "%sourceDir%\exploration.version" "%sourceDir%\mod_info.json" "%sourceDir%\icon.png"
"%sevenZip%" x -bb0 "%outputDir%" -o"%extractDir%"
:: set the size of the zip file and the extracted directory
for %%A in ("%outputDir%") do set "size=%%~zA"
set /a "sizeMB=%size% / 1024 / 1024"
set /a "sizeRemainder=%size% %% (1024 * 1024) * 1000 / (1024 * 1024)"
echo Size of the zip file is %sizeMB%.%sizeRemainder% MB

:: set the size of the extracted directory and display it
for /r "%extractDir%" %%A in (*) do set /a "dirSize+=%%~zA"
set /a "dirSizeMB=%dirSize% / 1024 / 1024"
set /a "dirSizeRemainder=%dirSize% %% (1024 * 1024) * 1000 / (1024 * 1024)"
echo Size of the extracted directory is %dirSizeMB%.%dirSizeRemainder% MB

:: delete the "outputDirVar" if it exists
@REM if exist "%outputDirVar%" rd /s /q "%outputDirVar%"

:: Initialize a counter
set /a "count=0"

:: Loop over the .zip files in reverse date order
for /f "delims=" %%A in ('dir /b /o-d /a-d "%outputDirVar%\*.zip"') do (
    :: Increment the counter
    set /a "count+=1"

    :: Skip the first file (the latest .zip file)
    if !count! gtr 1 (
        :: Delete the file
        del "%outputDirVar%\%%A"
    )
)

endlocal
pause
[close]

Edit the following lines to suit your needs:
location of 7zip.exe
Code
set "sevenZip=C:\Program Files\7-Zip\7z.exe"
your mods working folder
Code
set "sourceDir=D:\Games\Space-4x\Starsector\mods\mymod"
folder where mod and zip will be extracted to
Code
set "modsDirectory=D:\Games\Space-4x\Starsector\mods\mymod\output"

under this you can pass the folders and files names that you want included in you mod zip
search for this line or goto line 97
Code
:: use the 7zip command to create a zip file from the source directory, files and folders are defined below:
Code
"%sevenZip%" a -tzip -bb1 "%outputDir%" "%sourceDir%\data" "%sourceDir%\graphics" "%sourceDir%\jars" "%sourceDir%\src" "%sourceDir%\mod_info.json" "%sourceDir%\icon.png"
to add any folder
"%sourceDir%\<foldername>"
to add any file
"%sourceDir%\<filename.txt>"

Note:
You must have a "mod_info.json" in your "sourceDir"
with the following syntax, you are free to change the numbers as you like
"version": { "major": "1", "minor": "0", "patch": "0" },
« Last Edit: April 28, 2024, 03:50:28 AM by Anexgohan »
Logged