Skip to content

Commit 4191419

Browse files
lordflordf
authored andcommitted
Add project files.
1 parent 94668a4 commit 4191419

File tree

3 files changed

+215
-0
lines changed

3 files changed

+215
-0
lines changed

RimCzechGitDownloader.cs

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
// See https://aka.ms/new-console-template for more information
2+
3+
using System;
4+
using System.IO;
5+
using System.IO.Compression;
6+
using System.Linq;
7+
using System.Net.Http;
8+
9+
10+
namespace LordFanger;
11+
public class RimCzechGitDownloader
12+
{
13+
private class Expansion
14+
{
15+
private readonly string _rimBase;
16+
private readonly string _name;
17+
private readonly string _checkPart;
18+
private readonly string _baseDir;
19+
private readonly string _languageDir;
20+
private readonly bool _exists;
21+
22+
public Expansion(string rimBase, string name) {
23+
_rimBase = rimBase;
24+
_name = name;
25+
_checkPart = $"/{name}/";
26+
_baseDir = Path.Combine(_rimBase, "Data", name, "Languages");
27+
_languageDir = Path.Combine(_baseDir, "Czech - git");
28+
_exists = Directory.Exists(_baseDir);
29+
}
30+
31+
public bool Exists => _exists;
32+
33+
public string LanguageDir => _languageDir;
34+
35+
public bool IsForExpansion(string fullName) => fullName.Contains(_checkPart, StringComparison.OrdinalIgnoreCase);
36+
37+
public string GetPathFor(string[] parts) {
38+
return Path.Combine(new[] { _languageDir }.Concat(parts.SkipWhile(s => !s.Equals(_name, StringComparison.OrdinalIgnoreCase)).Skip(1)).ToArray());
39+
}
40+
41+
public static Expansion[] GetExpansions(string rimBase, out Expansion core) {
42+
core = new Expansion(rimBase, "Core");
43+
return new Expansion[] {
44+
core,
45+
new Expansion(rimBase, "Royalty"),
46+
new Expansion(rimBase, "Ideology"),
47+
new Expansion(rimBase, "Biotech"),
48+
};
49+
}
50+
}
51+
52+
private static string Version => "1.4";
53+
54+
private static void Main(string[] args) {
55+
Console.WriteLine($"Rim Czech Git Downloader - v {Version}");
56+
Console.WriteLine("Hledám instalaci RimWorldu...");
57+
if(args?.Length > 0)
58+
{
59+
SafeExecute(() => CheckForRimworldFolder(args[0]));
60+
}
61+
SafeExecute(() => CheckForRimworldFolder(@".\"));
62+
SafeExecute(() => CheckForSteamFolder(@"C:\Program Files\Steam\steamapps"));
63+
SafeExecute(() => CheckForSteamFolder(@"C:\Program Files (x86)\Steam\steamapps"));
64+
65+
var drives = DriveInfo.GetDrives();
66+
foreach(var drive in drives)
67+
{
68+
Console.WriteLine(drive.RootDirectory);
69+
CheckFolder(drive.RootDirectory.FullName, 0);
70+
}
71+
72+
void CheckFolder(string path, int level) {
73+
var directoryName = Path.GetFileName(path);
74+
if(directoryName == "steamapps")
75+
{
76+
CheckForSteamFolder(path);
77+
}
78+
79+
if(level > 3) return;
80+
81+
SafeExecute(
82+
() => {
83+
var subDirectories = Directory.GetDirectories(path);
84+
foreach(var subDirectory in subDirectories)
85+
{
86+
SafeExecute(() => CheckFolder(subDirectory, level + 1));
87+
}
88+
});
89+
}
90+
91+
void CheckForSteamFolder(string path) {
92+
SafeExecute(
93+
() => {
94+
var rimFolder = Path.Combine(path, "common", "RimWorld");
95+
CheckForRimworldFolder(rimFolder);
96+
});
97+
}
98+
99+
void SafeExecute(Action action) {
100+
try
101+
{
102+
action();
103+
} catch { }
104+
}
105+
106+
void CheckForRimworldFolder(string rimFolder) {
107+
if(!CheckRimworldFolder(rimFolder, out var expansions)) return;
108+
109+
Console.WriteLine("Nalezena instalace RimWorldu.");
110+
Console.WriteLine(rimFolder);
111+
ProcessRimworld(expansions);
112+
}
113+
114+
bool CheckRimworldFolder(string rimFolder, out Expansion[] expansions) {
115+
expansions = null;
116+
if(!Directory.Exists(rimFolder)) return false;
117+
118+
expansions = Expansion.GetExpansions(rimFolder, out var core);
119+
if(!core.Exists) return false;
120+
return true;
121+
}
122+
123+
void ProcessRimworld(Expansion[] expansions) {
124+
try
125+
{
126+
Console.WriteLine("Stahují aktuální překlad češtiny.");
127+
var zipStream = GetGitZip();
128+
using var zipArchive = new ZipArchive(zipStream);
129+
130+
Console.WriteLine("Mažu původní překlad.");
131+
foreach(var expansion in expansions)
132+
{
133+
if(!expansion.Exists) continue;
134+
if(Directory.Exists(expansion.LanguageDir)) Directory.Delete(expansion.LanguageDir);
135+
}
136+
137+
Console.WriteLine("Kopíruji aktuální překlad češtiny do hry.");
138+
foreach(var entry in zipArchive.Entries)
139+
{
140+
if(string.IsNullOrEmpty(entry.Name)) continue;
141+
var fullName = entry.FullName;
142+
var parts = fullName.Split('/', StringSplitOptions.RemoveEmptyEntries);
143+
144+
foreach(var expansion in expansions)
145+
{
146+
if(!expansion.IsForExpansion(fullName)) continue;
147+
var path = expansion.GetPathFor(parts);
148+
CopyToFile(entry, path);
149+
Console.WriteLine(path);
150+
break;
151+
}
152+
}
153+
154+
Console.WriteLine("Aktuální překlad do češtiny nakopírován.");
155+
Environment.Exit(0);
156+
157+
} catch(Exception ex)
158+
{
159+
Console.WriteLine($"{ex.GetType()}: {ex.Message}");
160+
Console.WriteLine(ex.StackTrace);
161+
Environment.Exit(1);
162+
}
163+
164+
void CopyToFile(ZipArchiveEntry entry, string path) {
165+
var directory = Path.GetDirectoryName(path);
166+
if(directory != null && !Directory.Exists(directory)) Directory.CreateDirectory(directory);
167+
entry.ExtractToFile(path);
168+
}
169+
}
170+
171+
Stream GetGitZip() {
172+
using var httpClient = new HttpClient();
173+
var response = httpClient.Send(new HttpRequestMessage(HttpMethod.Get, @"https://github.com/Ludeon/RimWorld-Czech/archive/refs/heads/master.zip"));
174+
using var data = response.Content.ReadAsStream();
175+
var ms = new MemoryStream();
176+
data.CopyTo(ms);
177+
ms.Position = 0;
178+
return ms;
179+
}
180+
}
181+
}

RimCzechGitDownloader.csproj

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net6.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
</PropertyGroup>
8+
9+
</Project>

RimCzechGitDownloader.sln

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.3.32922.545
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RimCzechGitDownloader", "RimCzechGitDownloader.csproj", "{B190781D-923D-4A9A-8C5D-D3EC722EB3ED}"
7+
EndProject
8+
Global
9+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
10+
Debug|Any CPU = Debug|Any CPU
11+
Release|Any CPU = Release|Any CPU
12+
EndGlobalSection
13+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
14+
{B190781D-923D-4A9A-8C5D-D3EC722EB3ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{B190781D-923D-4A9A-8C5D-D3EC722EB3ED}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{B190781D-923D-4A9A-8C5D-D3EC722EB3ED}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{B190781D-923D-4A9A-8C5D-D3EC722EB3ED}.Release|Any CPU.Build.0 = Release|Any CPU
18+
EndGlobalSection
19+
GlobalSection(SolutionProperties) = preSolution
20+
HideSolutionNode = FALSE
21+
EndGlobalSection
22+
GlobalSection(ExtensibilityGlobals) = postSolution
23+
SolutionGuid = {C6ECB1EF-6957-4259-B16C-8C8FA2427678}
24+
EndGlobalSection
25+
EndGlobal

0 commit comments

Comments
 (0)