-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileUtils.cs
122 lines (111 loc) · 3.4 KB
/
FileUtils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
using System;
using System.Diagnostics;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using Cardamom.Serialization;
using Cardamom.Utilities.Markov;
using SFML.Graphics;
namespace PanzerBlitz
{
public static class FileUtils
{
public static T Print<T>(T Object)
{
Console.WriteLine(Object);
return Object;
}
public static FileStream GetStream(string Path, FileMode FileMode, int Timeout)
{
var time = Stopwatch.StartNew();
while (time.ElapsedMilliseconds < Timeout)
{
try
{
return new FileStream(Path, FileMode);
}
catch (IOException e)
{
if (e.HResult != -2147024864)
throw;
}
}
throw new TimeoutException($"Failed to get a handle to {Path} within {Timeout}ms.");
}
public static void SerializeColor(SerializationOutputStream Stream, Color Color)
{
Stream.Write(Color.R);
Stream.Write(Color.G);
Stream.Write(Color.B);
Stream.Write(Color.A);
}
public static Color DeserializeColor(SerializationInputStream Stream)
{
return new Color(Stream.ReadByte(), Stream.ReadByte(), Stream.ReadByte(), Stream.ReadByte());
}
public static MarkovGenerator<char> LoadLanguage(string Path)
{
using (FileStream fileStream = new FileStream(Path, FileMode.Open))
{
using (GZipStream compressionStream = new GZipStream(fileStream, CompressionMode.Decompress))
{
var stream = new SerializationInputStream(compressionStream);
return new MarkovGenerator<char>(stream);
}
}
}
public static MarkovGenerator<char> GenerateLanguage(uint PrefixLength, string ExamplePath)
{
var g = new MarkovGenerator<char>(PrefixLength);
var parentheticals = new Regex("\\(.*\\)");
var capitals = new Regex("[A-Z- ]*");
foreach (var line in File.ReadAllLines(ExamplePath, Encoding.UTF8))
{
var name = line.ToLower();
if (name.Length == 0) continue;
name = parentheticals.Replace(name, "");
name = capitals.Replace(name, "");
name = name.Trim();
g.AddSequence(name);
}
return g;
}
public static void MungeLanguage(uint PrefixLength, string ExamplePath, string OutputPath)
{
var g = GenerateLanguage(PrefixLength, ExamplePath);
var random = new Random();
for (int i = 0; i < 20; ++i) Console.WriteLine(new string(g.Generate(random).ToArray()));
using (FileStream fileStream = new FileStream(OutputPath, FileMode.Create))
{
using (GZipStream compressionStream = new GZipStream(fileStream, CompressionLevel.Optimal))
{
var stream = new SerializationOutputStream(compressionStream);
stream.Write(g);
}
}
}
public static void Remap()
{
foreach (string file in Directory.EnumerateFiles("./Maps/"))
{
Map map;
using (FileStream stream = new FileStream(file, FileMode.Open))
{
using (GZipStream compressionStream = new GZipStream(stream, CompressionMode.Decompress))
{
map = new Map(new SerializationInputStream(compressionStream), null, new IdGenerator());
}
}
using (FileStream stream = new FileStream(file, FileMode.OpenOrCreate))
{
using (GZipStream compressionStream = new GZipStream(stream, CompressionLevel.Optimal))
{
map.Serialize(new SerializationOutputStream(compressionStream));
}
}
}
}
}
}