diff --git a/config.cmd b/config.cmd deleted file mode 100644 index efed2a70..00000000 --- a/config.cmd +++ /dev/null @@ -1,51 +0,0 @@ -@REM Script for noobs that have hard time reading instructions. -@echo off - -echo Configuring started... -set CURRENT_PATH=%~dp0 - -where /q git -if ERRORLEVEL 1 ( - echo ERROR: Git command line tool is not installed. Please install it: https://git-scm.com/download/win - pause - exit 1 -) - -rmdir /s /q %CURRENT_PATH%src\assets 2> NUL -rmdir /s /q %CURRENT_PATH%src\vendor 2> NUL -rmdir /s /q %CURRENT_PATH%src\db 2> NUL - -git clone --depth=1 https://github.com/ko4life-net/ko-assets %CURRENT_PATH%src\assets -git clone --depth=1 https://github.com/ko4life-net/ko-vendor %CURRENT_PATH%src\vendor -git clone https://github.com/ko4life-net/ko-db %CURRENT_PATH%src\db - -@REM src\All.sln - -echo: -echo: -echo Configuring done. -echo: -echo You may want to to start the src\All.sln Visual Studio solution to start developing. -echo: -echo If you haven't setup the database just yet, one last manual step is needed (assuming you have any version of MS-SQL server installed): -echo 1. Run the powershell script under src\db\import.ps1 to create and import the database into your SQL server. -echo 1.1. If you installed your SQL server as Named Instance, you may need to modify the server name in the script to yours. -echo 2. Setup odbcad as usual targeting kodb database name and your SQL server so that the server files can access the database. -echo 2.1. Note that you don't need to mess with the default configurations just to keep things simple. -echo: -echo: -echo Thank you and happy coding! -echo: -echo: - -pause - -@REM Here is registry an example of my current odbcad settings: -@REM [HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\ODBC\ODBC.INI\kodb] -@REM "Driver"="c:\\Windows\\SysWOW64\\sqlncli10.dll" -@REM "Server"="STEVEW\\SQLEXPRESS2008" -@REM "Database"="kodb" -@REM "Language"="us_english" -@REM "LastUser"="User" -@REM "Trusted_Connection"="Yes" -@REM "AutoTranslate"="No" diff --git a/config.ps1 b/config.ps1 new file mode 100644 index 00000000..51662c6e --- /dev/null +++ b/config.ps1 @@ -0,0 +1,75 @@ +Write-Output "Configuring started..." +$root_dir = Split-Path $MyInvocation.MyCommand.Path + +# This makes M$ Invoke-WebRequest and Expand-Archive work 4 times faster o.O +$global:ProgressPreference = "SilentlyContinue" + +$packages_dir = "$env:APPDATA\ko4life" +if (-not (Get-Command git -ErrorAction Ignore)) { + if (-not (Test-Path "$packages_dir\git")) { + Write-Output "INFO: Git not found. Installing portable version at: $packages_dir" + $tmp_dir = (New-TemporaryFile | ForEach-Object{ Remove-Item $_; mkdir $_ }).FullName + Invoke-WebRequest ` + "https://github.com/git-for-windows/git/releases/download/v2.39.1.windows.1/MinGit-2.39.1-busybox-32-bit.zip" ` + -OutFile "$tmp_dir\PortableGit.zip" + Remove-Item "$packages_dir\git" -Recurse -Force -ErrorAction Ignore + Expand-Archive "$tmp_dir\PortableGit.zip" -DestinationPath "$packages_dir\git" -Force + $env:Path = "$packages_dir\git\cmd;$env:Path" + } else { + $env:Path = "$packages_dir\git\cmd;$env:Path" + } + if (-not (Get-Command git -ErrorAction Ignore)) { + Write-Output "ERROR: Failed to install portable git..." + pause + exit(1) + } +} + +if (-not (Get-Command python, pip, virtualenv -ErrorAction Ignore)) { + if (-not (Test-Path "$packages_dir\python")) { + Write-Output "INFO: Python not found. Installng portable version at: $packages_dir" + $tmp_dir = (New-TemporaryFile | ForEach-Object{ Remove-Item $_; mkdir $_ }).FullName + Invoke-WebRequest ` + "https://www.python.org/ftp/python/3.11.1/python-3.11.1-embed-win32.zip" ` + -OutFile "$tmp_dir\PortablePython.zip" + Invoke-WebRequest ` + "https://bootstrap.pypa.io/get-pip.py" ` + -OutFile "$tmp_dir\get-pip.py" + + Remove-Item "$packages_dir\python" -Recurse -Force -ErrorAction Ignore + Expand-Archive "$tmp_dir\PortablePython.zip" -DestinationPath "$packages_dir\python" -Force + Remove-Item "$packages_dir\python\python3*._pth" -Force -ErrorAction Ignore + $env:Path = "$packages_dir\python;$packages_dir\python\Scripts;$env:Path" + python "$tmp_dir\get-pip.py" + pip install virtualenv + } else { + $env:Path = "$packages_dir\python;$packages_dir\python\Scripts;$env:Path" + } + if (-not (Get-Command python, pip, virtualenv -ErrorAction Ignore)) { + Write-Output "ERROR: Failed to install portable python..." + pause + exit(1) + } +} + +Remove-Item "$root_dir\script\venv" -Recurse -Force -ErrorAction Ignore +virtualenv "$root_dir\script\venv" +& "$root_dir\script\venv\Scripts\activate.ps1" +pip install -r "$root_dir\script\requirements.txt" +pip install "$root_dir\script" + +# TODO: Modular this in python code +# As well as the export.ps1 and import.ps1 scripts for db, so that it can be managed in one place +Remove-Item "$root_dir\src\assets" -Recurse -Force -ErrorAction Ignore +Remove-Item "$root_dir\src\vendor" -Recurse -Force -ErrorAction Ignore +Remove-Item "$root_dir\src\db" -Recurse -Force -ErrorAction Ignore + +git clone --depth=1 https://github.com/ko4life-net/ko-assets "$root_dir\src\assets" +git clone --depth=1 https://github.com/ko4life-net/ko-vendor "$root_dir\src\vendor" +git clone https://github.com/ko4life-net/ko-db "$root_dir\src\db" + +Write-Output "`n`nConfiguring successfully done.`n" +Write-Output "You may want to to start the src\All.sln Visual Studio solution to start developing." +Write-Output "`n`nThank you and happy coding!`n" + +pause diff --git a/odbcad.ps1 b/odbcad.ps1 new file mode 100644 index 00000000..90615be2 --- /dev/null +++ b/odbcad.ps1 @@ -0,0 +1,47 @@ +$server = "localhost" +$database = "kodb" + +$drivers = Get-OdbcDriver | Where-Object { $_.Name -like "*SQL Server*" -and $_.Platform -eq "32-bit" } +if (!$drivers) { + Write-Output "Are you sure SQL Server is installed? I couldn't find any drivers." + exit(1) +} + +$selected_driver = $null +if ($drivers.Count -eq 1) { + $selected_driver = $drivers[0] +} else { + while (!$selected_driver) { + Write-Output "Select the desired SQL Driver:" + for ($i = 0; $i -lt $drivers.Count; $i++) { + Write-Output "$($i+1). $($drivers[$i].Name)" + } + + $user_input = -1 + $input_valid = [int]::TryParse((Read-Host "Enter the number of the driver you want to select"), [ref]$user_input) + if (-not $input_valid -or $user_input -lt 1 -or $user_input -gt $drivers.Count) { + Write-Output "Invalid selection.`n" + } else { + $selected_driver = $drivers[$user_input - 1] + Write-Output "Selected SQL Driver: $($selected_driver.Name)`n" + break + } + } +} + +Remove-OdbcDsn -Name $database -DsnType "User" -ErrorAction Ignore +Add-OdbcDsn -Name $database -DriverName $selected_driver.Name -DsnType "User" -SetPropertyValue @("Server=$server", "Database=$database", "AutoTranslate=No", "Trusted_Connection=Yes") + +# Test connection +$con = New-Object System.Data.Odbc.OdbcConnection +$con.ConnectionString = "DSN=$database" +$con.Open() +$is_successful = $con.State -eq "Open" +$con.Close() +if ($is_successful) { + Write-Output "Successfully created odbcad connection driver and tested connection!" +} else { + Write-Output "Failed to test connection. Check that you first imported the database." + Write-Output "If that didn't work, depending on how you installed MSSQL (Default or Named Instance), you may need to change the server above from localhost to yours." + exit(1) +} diff --git a/script/.gitignore b/script/.gitignore new file mode 100644 index 00000000..361ea6df --- /dev/null +++ b/script/.gitignore @@ -0,0 +1,5 @@ +venv/ +build/ +ko.egg-info/ +__pycache__/ +*.pyc diff --git a/script/.pep8 b/script/.pep8 new file mode 100644 index 00000000..31d30358 --- /dev/null +++ b/script/.pep8 @@ -0,0 +1,5 @@ +[pycodestyle] +indent-size = 2 +recursive = true +aggressive = 0 +max_line_length = 140 diff --git a/script/README.md b/script/README.md new file mode 100644 index 00000000..460a1a89 --- /dev/null +++ b/script/README.md @@ -0,0 +1,35 @@ +# Knight Online Scripting + +Scripting project for Knight Online for the purpose of writing simple and portable scripts to help managing this project better. + +Note that you do not have to install stuff by yourself (unless you prefer), because the config.ps1 in the root dir of the project will setup a portable python installation for you. + +Some useful command below: +```sh +# assuming you're inside the script dir, even though not needed +cd script + +# Install requirements +virtualenv venv +& venv/Scripts/activate.ps1 +pip install -r requirements.txt +pip install . + +# Test that all works +ko --help + +# or +python ko/main.py --help + +# add `--editable` if you want to run the shorter `ko` command whilst editing the scripts: +pip install --editable . + +# Run some useful command +ko --help +ko build --config debug --project game --project server +ko db --action import +ko format + +# format your code before submitting a PR +autopep8 -i . --exclude venv +``` diff --git a/script/ko/__init__.py b/script/ko/__init__.py new file mode 100644 index 00000000..620d5ad6 --- /dev/null +++ b/script/ko/__init__.py @@ -0,0 +1,3 @@ + +def util_entry(**args): + print(f"Using clang-format to format code in the following directory: {args['dir']}") diff --git a/script/ko/build/__init__.py b/script/ko/build/__init__.py new file mode 100644 index 00000000..995ce778 --- /dev/null +++ b/script/ko/build/__init__.py @@ -0,0 +1,4 @@ +from .project import build_project + +def build_entry(**args): + build_project(**args) diff --git a/script/ko/build/project.py b/script/ko/build/project.py new file mode 100644 index 00000000..1c743095 --- /dev/null +++ b/script/ko/build/project.py @@ -0,0 +1,4 @@ + +def build_project(**args): + print(f"Building {args['config']} projects (cores: {args['cores']}): {list(args['project'])}") + print(args) diff --git a/script/ko/db/__init__.py b/script/ko/db/__init__.py new file mode 100644 index 00000000..7c1c8dae --- /dev/null +++ b/script/ko/db/__init__.py @@ -0,0 +1,3 @@ + +def db_entry(**args): + print(f"Database action: {args['action']}") diff --git a/script/ko/db/export.py b/script/ko/db/export.py new file mode 100644 index 00000000..e69de29b diff --git a/script/ko/db/import.py b/script/ko/db/import.py new file mode 100644 index 00000000..e69de29b diff --git a/script/ko/db/odbcad.py b/script/ko/db/odbcad.py new file mode 100644 index 00000000..e69de29b diff --git a/script/ko/main.py b/script/ko/main.py new file mode 100644 index 00000000..6a2a26cf --- /dev/null +++ b/script/ko/main.py @@ -0,0 +1,41 @@ +import os +import click + +from ko.build import build_entry +from ko.db import db_entry +from ko.util import util_entry + + +class CaseInsensitiveChoice(click.Choice): + def convert(self, value, param, ctx): + return super().convert(value.lower(), param, ctx) + + +@click.group() +def cli(): + pass + + +@cli.command() +@click.option('--config', '-c', type=CaseInsensitiveChoice(['release', 'debug']), default='release', help='Build configuration mode') +@click.option('--project', '-p', type=CaseInsensitiveChoice(['all', 'game', 'server', 'tool']), + default=['all'], multiple=True, help='Multiple projects to build') +@click.option('--cores', '-j', type=click.INT, default=os.cpu_count(), help='CPU cores count for building') +def build(**args): + build_entry(**args) + + +@cli.command() +@click.option('--action', '-a', type=CaseInsensitiveChoice(['import', 'export']), required=True, help='Import or export database') +def db(**args): + db_entry(**args) + + +@cli.command() +@click.option('--dir', '-d', type=click.STRING, help='Path to a directory to apply code formatting') +def format(**args): + util_entry(**args) + + +if __name__ == '__main__': + cli() diff --git a/script/ko/util/__init__.py b/script/ko/util/__init__.py new file mode 100644 index 00000000..620d5ad6 --- /dev/null +++ b/script/ko/util/__init__.py @@ -0,0 +1,3 @@ + +def util_entry(**args): + print(f"Using clang-format to format code in the following directory: {args['dir']}") diff --git a/script/ko/util/format.py b/script/ko/util/format.py new file mode 100644 index 00000000..e69de29b diff --git a/script/requirements.txt b/script/requirements.txt new file mode 100644 index 00000000..a282ae7a --- /dev/null +++ b/script/requirements.txt @@ -0,0 +1,5 @@ +autopep8 +click +mssql-scripter +pipreqs +pyodbc diff --git a/script/setup.py b/script/setup.py new file mode 100644 index 00000000..fba4d760 --- /dev/null +++ b/script/setup.py @@ -0,0 +1,14 @@ +from setuptools import setup, find_packages + +setup( + name='ko', + version='1.0.0', + packages=find_packages(), + include_package_data=True, + install_requires=['click'], + entry_points={ + 'console_scripts': [ + 'ko = ko.main:cli', + ], + }, +)