Skip to content

Commit efc4e1f

Browse files
authored
Added project files.
0 parents  commit efc4e1f

20 files changed

+2250
-0
lines changed

Extensions/ByteArrayEx.cs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
using System.Text;
20+
21+
public static class ByteArrayEx
22+
{
23+
public static string Prettify(this byte?[] byteArray, string wildcard = "??")
24+
{
25+
StringBuilder sb = new StringBuilder();
26+
27+
foreach (var b in byteArray)
28+
{
29+
if (b.HasValue)
30+
{
31+
sb.AppendFormat("{0:X2} ", b.Value);
32+
} else
33+
{
34+
sb.AppendFormat("{0} ", wildcard);
35+
}
36+
}
37+
38+
return sb.ToString().Trim();
39+
}
40+
}
41+
}

Extensions/ListEx.cs

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
public static class ListEx
20+
{
21+
public static Dictionary<TKey, List<TValue>> GroupByKey<TKey, TValue>(this IEnumerable<TValue> list, Func<TValue, TKey> keySelector)
22+
where TKey : notnull
23+
{
24+
Dictionary<TKey, List<TValue>> dictionary = new Dictionary<TKey, List<TValue>>();
25+
26+
foreach (TValue value in list)
27+
{
28+
var key = keySelector(value);
29+
30+
if (dictionary.ContainsKey(key))
31+
{
32+
dictionary[key].Add(value);
33+
} else
34+
{
35+
dictionary[key] = new List<TValue> { value };
36+
}
37+
}
38+
39+
return dictionary;
40+
}
41+
}
42+
}

Extensions/StringEx.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
public static class StringEx
20+
{
21+
public static string UppercaseFirstLetter(this string input)
22+
{
23+
if (string.IsNullOrEmpty(input))
24+
return input;
25+
26+
string lowercase = input.ToLower();
27+
char firstChar = char.ToUpper(lowercase[0]);
28+
return firstChar + lowercase.Substring(1);
29+
}
30+
}
31+
}

GDC.csproj

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
<Nullable>enable</Nullable>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<PackageReference Include="Spectre.Console" Version="0.49.1" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<Using Include="System.Runtime.InteropServices" />
16+
<Using Include="System.Text.Json" />
17+
<Using Include="System.Text.Json.Serialization" />
18+
</ItemGroup>
19+
20+
</Project>

GDC.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.9.34526.213
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "GDC", "GDC.csproj", "{5519DA7C-90FB-433C-A647-2F8A3ECB9358}"
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+
{5519DA7C-90FB-433C-A647-2F8A3ECB9358}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
15+
{5519DA7C-90FB-433C-A647-2F8A3ECB9358}.Debug|Any CPU.Build.0 = Debug|Any CPU
16+
{5519DA7C-90FB-433C-A647-2F8A3ECB9358}.Release|Any CPU.ActiveCfg = Release|Any CPU
17+
{5519DA7C-90FB-433C-A647-2F8A3ECB9358}.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 = {491C9520-C80E-4905-B8BF-C660D5AB7A5D}
24+
EndGlobalSection
25+
EndGlobal

Models/Binary/BinaryFile.cs

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
public class BinaryFile : IDisposable
20+
{
21+
public string FilePath { get; private set; }
22+
23+
public string FileName { get; private set; }
24+
25+
private byte[]? Data { get; set; }
26+
27+
private nint NativeHandle { get; set; } = nint.Zero;
28+
29+
public BinaryFile(string path)
30+
{
31+
this.FilePath = path;
32+
this.FileName = Path.GetFileName(this.FilePath);
33+
}
34+
35+
public bool FindExport(string symbol, Action<nint>? callback = null)
36+
{
37+
if (this.NativeHandle == nint.Zero)
38+
{
39+
if (NativeLibrary.TryLoad(this.FilePath, out nint handle))
40+
{
41+
this.NativeHandle = handle;
42+
} else
43+
{
44+
throw new Exception($"Unable to load native library '{this.FileName}'");
45+
}
46+
}
47+
48+
nint export = NativeLibrary.GetExport(this.NativeHandle, symbol);
49+
callback?.Invoke(export);
50+
return export != nint.Zero;
51+
}
52+
53+
public async Task ReadBytesAsync()
54+
{
55+
this.Data = await File.ReadAllBytesAsync(this.FilePath);
56+
Log.Debug($"Parsed binary '[bold slateblue1]{this.FileName}[/]' ({this.Data.Length} bytes)");
57+
}
58+
59+
public bool IsLoaded()
60+
{
61+
return this.Data != null;
62+
}
63+
64+
public bool FindPattern(byte?[] pattern, Action<long>? callback = null)
65+
{
66+
if (this.Data == null)
67+
{
68+
return false;
69+
}
70+
71+
string patternPrettified = pattern.Prettify();
72+
int count = 0;
73+
74+
Log.Debug($"Searching for pattern '[bold lightskyblue1]{patternPrettified}[/]' in binary '[bold slateblue1]{this.FileName}[/]'...");
75+
76+
for (long i = 0; i <= this.Data.LongLength - pattern.LongLength; i++)
77+
{
78+
long j;
79+
80+
for (j = 0; j < pattern.LongLength; j++)
81+
{
82+
if (pattern[j].HasValue && this.Data[i + j] != pattern[j])
83+
break;
84+
}
85+
86+
if (j == pattern.LongLength)
87+
{
88+
callback?.Invoke(i);
89+
count++;
90+
91+
Log.Debug($"Found pattern '[bold lightskyblue1]{patternPrettified}[/]' in binary '[bold slateblue1]{this.FileName}[/]' at offset [bold lightskyblue1]0x{i:X}[/] ([]{count}[/]. match)");
92+
}
93+
}
94+
95+
return count > 0;
96+
}
97+
98+
public void Dispose()
99+
{
100+
this.Data = null;
101+
}
102+
}
103+
}

Models/Binary/GameBinary.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
public class GameBinary : BinaryFile
20+
{
21+
public string Name { get; set; }
22+
23+
public OSPlatform Platform { get; set; }
24+
25+
public GameBinary(string name, string path, OSPlatform platform) : base(path)
26+
{
27+
this.Name = name;
28+
this.Platform = platform;
29+
}
30+
}
31+
}

Models/GameData/GameData.cs

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* This file is part of CounterStrikeSharp.
3+
* CounterStrikeSharp is free software: you can redistribute it and/or modify
4+
* it under the terms of the GNU General Public License as published by
5+
* the Free Software Foundation, either version 3 of the License, or
6+
* (at your option) any later version.
7+
*
8+
* CounterStrikeSharp is distributed in the hope that it will be useful,
9+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
10+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11+
* GNU General Public License for more details.
12+
*
13+
* You should have received a copy of the GNU General Public License
14+
* along with CounterStrikeSharp. If not, see <https://www.gnu.org/licenses/>. *
15+
*/
16+
17+
namespace GDC
18+
{
19+
using System.Collections.Concurrent;
20+
21+
public class GameData : ConcurrentDictionary<string, GameDataValue>
22+
{
23+
public IEnumerable<GameDataSignature> GetSignatures()
24+
{
25+
return (IEnumerable<GameDataSignature>)this.Where(d => d.Value.Offsets == null && d.Value.Signatures != null).ToList();
26+
}
27+
28+
public IEnumerable<GameDataSignature> GetSignatures(OSPlatform platform)
29+
{
30+
return this.GetSignatures().Where(s => s.IsValid(platform));
31+
}
32+
33+
public IEnumerable<GameDataOffset> GetOffsets()
34+
{
35+
return (IEnumerable<GameDataOffset>)this.Where(d => d.Value.Offsets != null && d.Value.Signatures == null).ToList();
36+
}
37+
}
38+
}

0 commit comments

Comments
 (0)