Skip to content

Commit

Permalink
Game File Loading/Saving System - Refractor of the GFXLibrary and File
Browse files Browse the repository at this point in the history
WARNING WIP - THERE WILL BE BUGS
MAJOR UPDATE
TODO: Try to reduce the file size - more the 7 times the game size is a bit much....

Added a saving and loading system for the ATD images. Now loads all files from the game and saves it to our project when used in the editor. It will save all ATD files to a .pck when it's a standalone build. That .pck will be loaded into the game for later use.

Added a loading scene, for picking the path to your ATD install.

Changed the main scene, from the base scene to the loading scene.
Added textures for testing purposes to the base scene.

Changed the system behind getting the textures from the GFXLibrary - now you have to use GFXFile.GetTexture() to retrieve a chacheble texture

Updated the README

Updated the background color to black, the window size to Full HD and the resizing method to stretch.
  • Loading branch information
WizzardMaker committed May 10, 2019
1 parent 4b1c04a commit 5018c10
Show file tree
Hide file tree
Showing 12 changed files with 427 additions and 115 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -269,5 +269,7 @@ ATD/
SDL*/
source_code_win/
BASE/
BUILDS/
.import/
.mono/
Images/
1 change: 1 addition & 0 deletions OpenATD.csproj
Expand Up @@ -50,6 +50,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="src\ATDGameLoader.cs" />
<Compile Include="src\MouseCursor.cs" />
<Compile Include="src\GFXLibrary.cs" />
</ItemGroup>
Expand Down
1 change: 0 additions & 1 deletion Properties/AssemblyInfo.cs
Expand Up @@ -11,7 +11,6 @@
[assembly: AssemblyCopyright("")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
Expand Down
7 changes: 6 additions & 1 deletion README.md
Expand Up @@ -12,7 +12,12 @@ It will add support for Full HD resolution, and native support for platforms lik
## How to compile the Godot branch
- Download Godot 3.1 Mono or later
- Install Airline Tycoon Deluxe
- Set the ATD Path variable in the BaseGameController from the base scene
- Run the project in godot for the first time WITHOUT opening any scenes, point it to the directory of the at.exe and let it extract the game files.
(The game images can't be posted on github due to copyright reasons. Thats why there are files missing inside the project. Opening scenes let's godot know that there are files missing and it will delete all references to those files. If we run the project before godot checks for files, all missing files will be added!)


## How to build the project
- TODO: Add the texture cleaner programm...

## WIP Pictures
All images are work and progress and do not represent any gameplay at the moment. All are still images taken from the game files
Expand Down
2 changes: 1 addition & 1 deletion default_env.tres
Expand Up @@ -3,5 +3,5 @@
[sub_resource type="ProceduralSky" id=1]

[resource]
background_mode = 2
background_mode = 1
background_sky = SubResource( 1 )
33 changes: 33 additions & 0 deletions export_presets.cfg
@@ -0,0 +1,33 @@
[preset.0]

name="Windows Desktop"
platform="Windows Desktop"
runnable=true
custom_features=""
export_filter="scenes"
export_files=PoolStringArray( "res://Properties/AssemblyInfo.cs", "res://default_env.tres", "res://icon.png", "res://scenes/base.tscn", "res://scenes/load.tscn", "res://src/GFXLibrary.cs", "res://src/MouseCursor.cs", "res://src/RoomManager.cs" )
include_filter=""
exclude_filter=""
export_path="P:/Projekte/Major Games/OpenATD/BUILDS/oATD.exe"
patch_list=PoolStringArray( )
script_export_mode=1
script_encryption_key=""

[preset.0.options]

texture_format/bptc=false
texture_format/s3tc=true
texture_format/etc=false
texture_format/etc2=false
texture_format/no_bptc_fallbacks=true
binary_format/64_bits=true
custom_template/release=""
custom_template/debug=""
application/icon=""
application/file_version=""
application/product_version=""
application/company_name=""
application/product_name=""
application/file_description=""
application/copyright=""
application/trademarks=""
10 changes: 9 additions & 1 deletion project.godot
Expand Up @@ -16,9 +16,17 @@ _global_script_class_icons={
[application]

config/name="OpenATD"
run/main_scene="res://scenes/base.tscn"
run/main_scene="res://scenes/load.tscn"
config/icon="res://icon.png"

[display]

window/size/width=1920
window/size/height=1080
window/stretch/mode="2d"
window/stretch/aspect="keep"

[rendering]

environment/default_clear_color=Color( 0, 0, 0, 1 )
environment/default_environment="res://default_env.tres"
29 changes: 26 additions & 3 deletions scenes/base.tscn
@@ -1,7 +1,30 @@
[gd_scene load_steps=2 format=2]
[gd_scene load_steps=5 format=2]

[ext_resource path="res://src/MouseCursor.cs" type="Script" id=1]
[ext_resource path="res://Images/gli/buero_a/BUERO.tres" type="Texture" id=2]
[ext_resource path="res://Images/gli/buero_a/LETTERS.tres" type="Texture" id=3]
[ext_resource path="res://Images/gli/buero_a/NOSABO.tres" type="Texture" id=4]

[node name="BaseGameController" type="Node2D"]
[node name="Game" type="Node2D"]

[node name="MOUSE" type="Sprite" parent="."]
position = Vector2( 494.348, 30.5995 )

[node name="Mouse" type="Sprite" parent="."]
position = Vector2( 366.758, 207.202 )
z_index = 999
centered = false
script = ExtResource( 1 )
pathToAirlineTycoonD = "..\\BASE\\ATD"

[node name="buero" type="Sprite" parent="."]
position = Vector2( 965.744, 539.596 )
scale = Vector2( 2.44196, 2.44196 )
texture = ExtResource( 2 )

[node name="letters" type="Sprite" parent="buero"]
position = Vector2( 69.4884, 176.542 )
texture = ExtResource( 3 )

[node name="nosabo" type="Sprite" parent="buero"]
position = Vector2( 107.3, 110.007 )
texture = ExtResource( 4 )
48 changes: 48 additions & 0 deletions scenes/load.tscn
@@ -0,0 +1,48 @@
[gd_scene load_steps=2 format=2]

[ext_resource path="res://src/ATDGameLoader.cs" type="Script" id=1]

[node name="Node2D" type="Node2D"]
script = ExtResource( 1 )

[node name="Title" type="Sprite" parent="."]
position = Vector2( 937.422, 541.914 )
scale = Vector2( 2.24936, 2.24936 )

[node name="FileDialog" type="FileDialog" parent="."]
margin_left = 685.0
margin_top = 388.863
margin_right = 1239.0
margin_bottom = 710.863
window_title = "Verzeichnis wählen"
dialog_text = "Select Path"
mode = 2
access = 2
current_dir = "P:/Projekte/Major Games/OpenATD"
current_path = "P:/Projekte/Major Games/OpenATD/"

[node name="DirectoryInvalid" type="AcceptDialog" parent="."]
anchor_left = 0.5
anchor_top = 0.5
anchor_right = 0.5
anchor_bottom = 0.5
margin_left = 731.76
margin_top = 511.715
margin_right = 1178.76
margin_bottom = 569.715
resizable = true
dialog_text = "\"INVALID PATH CHOSEN, CAN'T FIND glbasis.gli IN SUBFOLDER gli!\""

[node name="LoadInfo" type="Label" parent="."]
anchor_right = 1.0
anchor_bottom = 1.0
margin_left = 261.299
margin_top = 1036.08
margin_right = 318.299
margin_bottom = 1050.08
rect_scale = Vector2( 2, 2 )
custom_colors/font_color = Color( 0.290196, 0.92549, 0, 1 )
custom_colors/font_color_shadow = Color( 0.0313726, 0.137255, 0.027451, 1 )
text = "Loading.."
align = 3
valign = 1
162 changes: 162 additions & 0 deletions src/ATDGameLoader.cs
@@ -0,0 +1,162 @@
using Godot;
using System;
using System.Linq;
using System.Collections.Generic;
using Directory = System.IO.Directory;
using Thread = System.Threading.Thread;

public class ATDGameLoader : Node {
public Label loadInfo;
public FileDialog selectATDPath;
public AcceptDialog directoryInvalidDialog;

static Dictionary<string, List<GFXLibrary>> libraryFolders;

[Export]
bool forceRebuild = false;
bool isInEditor;

static volatile string currentFolder;
static volatile string currentLibrary;
static volatile string currentFile;

// Called when the node enters the scene tree for the first time.
public override void _Ready() {
loadInfo = GetNode<Label>("LoadInfo");

isInEditor = Engine.EditorHint;

if (forceRebuild || (!System.IO.File.Exists("ATDFiles.pck")) || !Directory.Exists(ProjectSettings.GlobalizePath("res://Images"))) {
selectATDPath = GetNode<FileDialog>("FileDialog"); //Get the path to the ATD install
selectATDPath.Connect("dir_selected", this, nameof(ChoseFile));
selectATDPath.PopupCentered(new Vector2(500, 500));

directoryInvalidDialog = GetNode<AcceptDialog>("DirectoryInvalid");
directoryInvalidDialog.Connect("confirmed", this, nameof(AcceptDialog));
} else { //If there is no need for a rebuild, just skip asking
if (!isInEditor) {
ProjectSettings.LoadResourcePack("ATDFiles.pck"); //If we are not in the editor: we need to add the ATDFiles
}
GetTree().ChangeScene("res://scenes/base.tscn");
}
}

public void ChoseFile(string dir) {
if (!System.IO.File.Exists(dir + "/gli/glbasis.gli")) { //Basic check to see if we are inside the ATD folder
GD.PrintErr("INVALID PATH CHOSEN, CAN'T FIND glbasis.gli IN SUBFOLDER gli!");
directoryInvalidDialog.PopupCentered();
return;
}

GFXLibrary.pathToAirlineTycoonD = dir;

Thread t = new Thread(LoadData);
t.Name = "DataLoader";

t.Start();
}

public void AcceptDialog() {
selectATDPath.PopupCentered(new Vector2(500, 500)); //try try and try again
}

public override void _Process(float delta) {
loadInfo.SetText("Loading: " + currentFolder + "/" + currentLibrary + "/" + currentFile);
}

private void LoadData() {
libraryFolders = new Dictionary<string, List<GFXLibrary>>(); //A list of graphic folders of the game
libraryFolders.Add("room", new List<GFXLibrary>());
libraryFolders.Add("gli", new List<GFXLibrary>());

foreach (var folderKVP in libraryFolders) {
string folderName = folderKVP.Key;
List<GFXLibrary> folderGFXList = folderKVP.Value;

currentFolder = folderName;

//Load up all GFX files in the directory
string[] files = System.IO.Directory.GetFiles(GFXLibrary.pathToAirlineTycoonD + "/" + folderName);
foreach (string f in files) {
if (f.EndsWith(".gli")) {
var lib = new GFXLibrary(GFXLibrary.pathToAirlineTycoonD + "/" + folderName + "/" + System.IO.Path.GetFileName(f));
lib.GetFilesInLibrary();

folderGFXList.Add(lib);
}
}
}


GetNode<Sprite>("Title").SetTexture(
libraryFolders["room"]
.Find((lib) => lib.name == "titel2").files
.Find((f) => f.name.Trim('\0') == "TITEL").GetTexture());


//Write them to disk
ExportImages(!System.IO.File.Exists("ATDFiles.pck") && !isInEditor);

//Should only be needed when we are not in the editor, but adding it doesnt hurt us, even if the file isn't present
bool s = ProjectSettings.LoadResourcePack("ATDFiles.pck");
GetTree().ChangeScene("res://scenes/base.tscn");
}

private void ExportImages(bool packFiles) {
PCKPacker p = new PCKPacker();


if (packFiles) //Should we pack them up? Only needed when we are not inside the editor
p.PckStart("ATDFiles.pck", 4);


string basePath = ProjectSettings.GlobalizePath("res://Images"); //Get an absolute Path to our project/executable folder
string baseGodotPath = "res://Images";

if (!Directory.Exists(basePath))
Directory.CreateDirectory(basePath);


foreach (var folderKVP in libraryFolders) {
List<GFXLibrary> librarys = folderKVP.Value;
string libraryName = folderKVP.Key;

SaveLibrarysInList(packFiles, p, basePath + "/" + libraryName, baseGodotPath + "/" + libraryName, librarys);
}
if (packFiles)
p.Flush(true);
}

private static void SaveLibrarysInList(bool packFiles, PCKPacker p, string basePath, string baseGodotPath, List<GFXLibrary> librarys) {
foreach (var lib in librarys) {
string libPath = basePath + "/" + lib.name;
string libGodotPath = baseGodotPath + "/" + lib.name;

currentLibrary = lib.name;

if (!Directory.Exists(libPath))
Directory.CreateDirectory(libPath);

SaveFiles(packFiles, p, lib, libPath, libGodotPath);
}
}

private static void SaveFiles(bool packFiles, PCKPacker p, GFXLibrary lib, string libPath, string libGodotPath) {
foreach (GFXLibrary.GFXFile file in lib.files) {
string fileName = file.name.Trim('\0') + ".tres";
string filePath = libPath + "/" + fileName;
string fileGodotPath = libGodotPath + "/" + fileName;

currentFile = fileName;

GD.Print(filePath);
GD.Print(fileGodotPath);

ResourceSaver.Save(filePath, file.GetTexture(), (int)ResourceSaver.SaverFlags.Compress);

if (packFiles)
p.AddFile(fileGodotPath, filePath);

}
}
}

0 comments on commit 5018c10

Please sign in to comment.