Skip to content

Commit 9c05753

Browse files
committed
Added some stuff
1 parent 36e160c commit 9c05753

File tree

8 files changed

+188
-9
lines changed

8 files changed

+188
-9
lines changed
Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
1-
using System;
1+
using DynmapFilesToSQLite.Converter.Reader;
2+
using DynmapFilesToSQLite.Converter.Reader.impl;
3+
using Microsoft.Data.Sqlite;
4+
using System;
25
using System.Collections.Generic;
36
using System.IO;
4-
using System.Linq;
5-
using System.Text;
6-
using System.Threading.Tasks;
77

88
namespace DynmapFilesToSQLite.Converter {
99
public class Converter {
10-
public static void Convert(DirectoryInfo tilesFolder, DirectoryInfo markersFolder, bool useJPG) {
10+
public static void Convert(DirectoryInfo tilesFolder, DirectoryInfo markersFolder, FileInfo outputFile, bool useJPG) {
11+
List<DynReader> readers = new List<DynReader>();
12+
readers.Add(new MarkerIconsReader(markersFolder));
13+
readers.Add(new FacesReader(new DirectoryInfo(Path.Combine(tilesFolder.FullName, "faces"))));
1114

15+
SQLiteWriter writer = new SQLiteWriter(outputFile);
16+
writer.CreateTables();
17+
18+
foreach(DynReader reader in readers) {
19+
SqliteTransaction transaction = writer.CreateTransaction();
20+
reader.ExecuteSqliteCommands(transaction);
21+
transaction.Commit();
22+
}
23+
24+
writer.Close();
1225
}
1326
}
1427
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using Microsoft.Data.Sqlite;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
6+
namespace DynmapFilesToSQLite.Converter.Reader {
7+
public abstract class DynReader {
8+
protected DirectoryInfo myDir;
9+
10+
public DynReader(DirectoryInfo myDir) {
11+
this.myDir = myDir;
12+
}
13+
14+
public abstract void ExecuteSqliteCommands(SqliteTransaction transaction);
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using Microsoft.Data.Sqlite;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace DynmapFilesToSQLite.Converter.Reader.impl {
8+
public class FacesReader : DynReader {
9+
public FacesReader(DirectoryInfo myDir) : base(myDir) { }
10+
11+
public override void ExecuteSqliteCommands(SqliteTransaction transaction) {
12+
13+
}
14+
}
15+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
using Microsoft.Data.Sqlite;
2+
using System;
3+
using System.Collections.Generic;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace DynmapFilesToSQLite.Converter.Reader.impl {
8+
public class MarkerIconsReader : DynReader {
9+
public MarkerIconsReader(DirectoryInfo myDir) : base(myDir) { }
10+
11+
public override void ExecuteSqliteCommands(SqliteTransaction transaction) {
12+
var images = myDir.EnumerateFiles().Where(file => file.Name.EndsWith(".png"));
13+
14+
foreach(FileInfo image in images) {
15+
SqliteCommand cmd = new SqliteCommand();
16+
cmd.CommandText = "INSERT INTO MarkerIcons VALUES (@Name, @FileBytes);";
17+
18+
string name = image.Name.Substring(0, image.Name.Length - 4);
19+
cmd.Parameters.AddWithValue("@Name", name);
20+
cmd.Parameters.AddWithValue("@FileBytes", File.ReadAllBytes(image.FullName));
21+
22+
cmd.Transaction = transaction;
23+
cmd.Connection = transaction.Connection;
24+
cmd.ExecuteNonQuery();
25+
26+
Console.WriteLine("Command executed for " + name);
27+
}
28+
}
29+
}
30+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using System.Threading.Tasks;
7+
using Microsoft.Data.Sqlite;
8+
9+
// Word Wrap is recommended
10+
namespace DynmapFilesToSQLite.Converter {
11+
class SQLiteWriter {
12+
private const string CREATE_TABLES_COMMANDS = "CREATE TABLE Faces (PlayerName STRING NOT NULL, TypeID INT NOT NULL, Image BLOB, PRIMARY KEY(PlayerName, TypeID));CREATE TABLE Maps (ID INTEGER PRIMARY KEY AUTOINCREMENT, WorldID STRING NOT NULL, MapID STRING NOT NULL, Variant STRING NOT NULL);CREATE TABLE MarkerFiles (FileName STRING PRIMARY KEY NOT NULL, Content CLOB);CREATE TABLE MarkerIcons (IconName STRING PRIMARY KEY NOT NULL, Image BLOB);CREATE TABLE SchemaVersion (level INT PRIMARY KEY NOT NULL);CREATE TABLE Tiles (MapID INT NOT NULL, x INT NOT NULL, y INT NOT NULL, zoom INT NOT NULL, HashCode INT NOT NULL, LastUpdate INT NOT NULL, Format INT NOT NULL, Image BLOB, PRIMARY KEY(MapID, x, y, zoom));";
13+
14+
public FileInfo sqliteFile;
15+
public SqliteConnection sql;
16+
17+
public SQLiteWriter(FileInfo sqliteFile) {
18+
this.sqliteFile = sqliteFile;
19+
20+
if(sqliteFile.Exists) {
21+
Console.WriteLine("Deleting old database...");
22+
sqliteFile.Delete();
23+
}
24+
sqliteFile.Create().Close();
25+
26+
SqliteConnectionStringBuilder connectionBuilder = new SqliteConnectionStringBuilder();
27+
connectionBuilder.DataSource = sqliteFile.FullName;
28+
connectionBuilder.Cache = SqliteCacheMode.Private;
29+
this.sql = new SqliteConnection(connectionBuilder.ToString());
30+
this.sql.Open();
31+
}
32+
33+
public void CreateTables() {
34+
SqliteCommand createCommands = new SqliteCommand(CREATE_TABLES_COMMANDS, this.sql);
35+
createCommands.ExecuteReader();
36+
}
37+
38+
public SqliteTransaction CreateTransaction() {
39+
this.sql.Open();
40+
return this.sql.BeginTransaction();
41+
}
42+
43+
public void Close() {
44+
this.sql.Close();
45+
}
46+
}
47+
}

DynmapFilesToSQLite/DynmapFilesToSQLite.csproj

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
1212
<FileAlignment>512</FileAlignment>
1313
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<NuGetPackageImportStamp>
15+
</NuGetPackageImportStamp>
1416
</PropertyGroup>
1517
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
1618
<PlatformTarget>AnyCPU</PlatformTarget>
@@ -35,11 +37,39 @@
3537
<StartupObject>DynmapFilesToSQLite.Program</StartupObject>
3638
</PropertyGroup>
3739
<ItemGroup>
40+
<Reference Include="Microsoft.Data.Sqlite, Version=3.1.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60, processorArchitecture=MSIL">
41+
<HintPath>..\packages\Microsoft.Data.Sqlite.Core.3.1.0\lib\netstandard2.0\Microsoft.Data.Sqlite.dll</HintPath>
42+
</Reference>
3843
<Reference Include="Mono.Options, Version=6.0.0.0, Culture=neutral, processorArchitecture=MSIL">
3944
<HintPath>..\packages\Mono.Options.6.6.0.161\lib\net40\Mono.Options.dll</HintPath>
4045
</Reference>
46+
<Reference Include="SQLitePCLRaw.batteries_v2, Version=2.0.2.669, Culture=neutral, PublicKeyToken=8226ea5df37bcae9, processorArchitecture=MSIL">
47+
<HintPath>..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.batteries_v2.dll</HintPath>
48+
</Reference>
49+
<Reference Include="SQLitePCLRaw.core, Version=2.0.2.669, Culture=neutral, PublicKeyToken=1488e028ca7ab535, processorArchitecture=MSIL">
50+
<HintPath>..\packages\SQLitePCLRaw.core.2.0.2\lib\netstandard2.0\SQLitePCLRaw.core.dll</HintPath>
51+
</Reference>
52+
<Reference Include="SQLitePCLRaw.nativelibrary, Version=2.0.2.669, Culture=neutral, PublicKeyToken=502ed628492ab262, processorArchitecture=MSIL">
53+
<HintPath>..\packages\SQLitePCLRaw.bundle_e_sqlite3.2.0.2\lib\net461\SQLitePCLRaw.nativelibrary.dll</HintPath>
54+
</Reference>
55+
<Reference Include="SQLitePCLRaw.provider.dynamic_cdecl, Version=2.0.2.669, Culture=neutral, PublicKeyToken=b68184102cba0b3b, processorArchitecture=MSIL">
56+
<HintPath>..\packages\SQLitePCLRaw.provider.dynamic_cdecl.2.0.2\lib\netstandard2.0\SQLitePCLRaw.provider.dynamic_cdecl.dll</HintPath>
57+
</Reference>
4158
<Reference Include="System" />
59+
<Reference Include="System.Buffers, Version=4.0.2.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
60+
<HintPath>..\packages\System.Buffers.4.4.0\lib\netstandard2.0\System.Buffers.dll</HintPath>
61+
</Reference>
4262
<Reference Include="System.Core" />
63+
<Reference Include="System.Memory, Version=4.0.1.1, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51, processorArchitecture=MSIL">
64+
<HintPath>..\packages\System.Memory.4.5.3\lib\netstandard2.0\System.Memory.dll</HintPath>
65+
</Reference>
66+
<Reference Include="System.Numerics" />
67+
<Reference Include="System.Numerics.Vectors, Version=4.1.3.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
68+
<HintPath>..\packages\System.Numerics.Vectors.4.4.0\lib\net46\System.Numerics.Vectors.dll</HintPath>
69+
</Reference>
70+
<Reference Include="System.Runtime.CompilerServices.Unsafe, Version=4.0.4.1, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
71+
<HintPath>..\packages\System.Runtime.CompilerServices.Unsafe.4.5.2\lib\netstandard2.0\System.Runtime.CompilerServices.Unsafe.dll</HintPath>
72+
</Reference>
4373
<Reference Include="System.Xml.Linq" />
4474
<Reference Include="System.Data.DataSetExtensions" />
4575
<Reference Include="Microsoft.CSharp" />
@@ -49,6 +79,10 @@
4979
</ItemGroup>
5080
<ItemGroup>
5181
<Compile Include="Converter\Converter.cs" />
82+
<Compile Include="Converter\Reader\DynReader.cs" />
83+
<Compile Include="Converter\Reader\impl\FacesReader.cs" />
84+
<Compile Include="Converter\Reader\impl\MarkerIconsReader.cs" />
85+
<Compile Include="Converter\SQLiteWriter.cs" />
5286
<Compile Include="Program.cs" />
5387
<Compile Include="Properties\AssemblyInfo.cs" />
5488
</ItemGroup>
@@ -57,4 +91,11 @@
5791
<None Include="packages.config" />
5892
</ItemGroup>
5993
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
94+
<Import Project="..\packages\SQLitePCLRaw.lib.e_sqlite3.2.0.2\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets" Condition="Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.2.0.2\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" />
95+
<Target Name="EnsureNuGetPackageBuildImports" BeforeTargets="PrepareForBuild">
96+
<PropertyGroup>
97+
<ErrorText>Dieses Projekt verweist auf mindestens ein NuGet-Paket, das auf diesem Computer fehlt. Verwenden Sie die Wiederherstellung von NuGet-Paketen, um die fehlenden Dateien herunterzuladen. Weitere Informationen finden Sie unter "http://go.microsoft.com/fwlink/?LinkID=322105". Die fehlende Datei ist "{0}".</ErrorText>
98+
</PropertyGroup>
99+
<Error Condition="!Exists('..\packages\SQLitePCLRaw.lib.e_sqlite3.2.0.2\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets')" Text="$([System.String]::Format('$(ErrorText)', '..\packages\SQLitePCLRaw.lib.e_sqlite3.2.0.2\build\net461\SQLitePCLRaw.lib.e_sqlite3.targets'))" />
100+
</Target>
60101
</Project>

DynmapFilesToSQLite/Program.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,28 +5,35 @@
55
namespace DynmapFilesToSQLite {
66
class Program {
77
static void Main(string[] args) {
8-
string tilesFolderPath = "";
8+
string tilesFolderPath = "", outputFilePath = "";
99
bool useJPGs = false;
1010

1111
OptionSet options = new OptionSet() {
1212
{ "tilesFolder=", arg => tilesFolderPath = arg },
13-
{ "useJPG", arg => useJPGs = arg != null }
13+
{ "useJPG", arg => useJPGs = arg != null },
14+
{ "outputFile=", arg => outputFilePath = arg }
1415
};
1516
options.Parse(args);
1617

18+
if(tilesFolderPath.Length == 0 || outputFilePath.Length == 0) {
19+
Console.WriteLine("Not all arguments provided");
20+
return;
21+
}
22+
1723
DirectoryInfo tilesFolder = new DirectoryInfo(tilesFolderPath);
1824
if(!tilesFolder.Exists) {
1925
Console.WriteLine("Tiles folder not found");
2026
return;
2127
}
2228

23-
DirectoryInfo markersFolder = new DirectoryInfo(Path.Combine(tilesFolder.FullName, "_markers_")));
29+
DirectoryInfo markersFolder = new DirectoryInfo(Path.Combine(tilesFolder.FullName, "_markers_"));
2430
if (!markersFolder.Exists) {
2531
Console.WriteLine("Tiles folder invalid");
2632
return;
2733
}
2834

29-
Converter.Converter.Convert(tilesFolder, markersFolder, useJPGs);
35+
FileInfo outputFile = new FileInfo(outputFilePath);
36+
Converter.Converter.Convert(tilesFolder, markersFolder, outputFile, useJPGs);
3037
}
3138
}
3239
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<packages>
3+
<package id="Microsoft.Data.Sqlite" version="3.1.0" targetFramework="net472" />
4+
<package id="Microsoft.Data.Sqlite.Core" version="3.1.0" targetFramework="net472" />
35
<package id="Mono.Options" version="6.6.0.161" targetFramework="net472" />
6+
<package id="SQLitePCLRaw.bundle_e_sqlite3" version="2.0.2" targetFramework="net472" />
7+
<package id="SQLitePCLRaw.core" version="2.0.2" targetFramework="net472" />
8+
<package id="SQLitePCLRaw.lib.e_sqlite3" version="2.0.2" targetFramework="net472" />
9+
<package id="SQLitePCLRaw.provider.dynamic_cdecl" version="2.0.2" targetFramework="net472" />
10+
<package id="System.Buffers" version="4.4.0" targetFramework="net472" />
11+
<package id="System.Memory" version="4.5.3" targetFramework="net472" />
12+
<package id="System.Numerics.Vectors" version="4.4.0" targetFramework="net472" />
13+
<package id="System.Runtime.CompilerServices.Unsafe" version="4.5.2" targetFramework="net472" />
414
</packages>

0 commit comments

Comments
 (0)