Skip to content

Commit

Permalink
Don't try to download sqlite DLLs on non-windows
Browse files Browse the repository at this point in the history
  • Loading branch information
UnknownShadow200 committed Aug 9, 2022
1 parent 2f803e1 commit 1fdc083
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 32 deletions.
3 changes: 2 additions & 1 deletion CLI/Program.cs
Expand Up @@ -217,7 +217,8 @@ public static class Program {
if (part.Length == 0) continue;
ConsoleColor color = GetConsoleColor(curCol);

if (color == ConsoleColor.White) {
if (color == ConsoleColor.White) {
// show in user's preferred console text color
Console.ResetColor();
} else {
Console.ForegroundColor = color;
Expand Down
13 changes: 9 additions & 4 deletions MCGalaxy/Database/Backends/MySQL.cs
Expand Up @@ -22,9 +22,10 @@
using System.Text;
using MySql.Data.MySqlClient;

namespace MCGalaxy.SQL {

public sealed class MySQLBackend : IDatabaseBackend {
namespace MCGalaxy.SQL
{
public sealed class MySQLBackend : IDatabaseBackend
{
public static IDatabaseBackend Instance = new MySQLBackend();
public MySQLBackend() {
// MySQL uses case insensitive collation by default
Expand All @@ -33,7 +34,8 @@ public sealed class MySQLBackend : IDatabaseBackend {
}

public override bool EnforcesTextLength { get { return true; } }
public override bool MultipleSchema { get { return true; } }
public override bool MultipleSchema { get { return true; } }
public override string EngineName { get { return "MySQL"; } }

internal override IDbConnection CreateConnection() {
const string format = "Data Source={0};Port={1};User ID={2};Password={3};Pooling={4};Treat Tiny As Boolean=false;";
Expand All @@ -51,6 +53,9 @@ public sealed class MySQLBackend : IDatabaseBackend {
}


public override void LoadDependencies() {
}

public override void CreateDatabase() {
string sql = "CREATE DATABASE if not exists `" + Server.Config.MySQLDatabaseName + "`";
Database.Do(sql, true, null, null);
Expand Down
31 changes: 25 additions & 6 deletions MCGalaxy/Database/Backends/SQLite.cs
Expand Up @@ -21,17 +21,19 @@
using System.IO;
using System.Text;

namespace MCGalaxy.SQL {

public sealed class SQLiteBackend : IDatabaseBackend {
namespace MCGalaxy.SQL
{
public sealed class SQLiteBackend : IDatabaseBackend
{
public static IDatabaseBackend Instance = new SQLiteBackend();
public SQLiteBackend() {
CaselessWhereSuffix = " COLLATE NOCASE";
CaselessLikeSuffix = " COLLATE NOCASE";
CaselessLikeSuffix = " COLLATE NOCASE";
}

public override bool EnforcesTextLength { get { return false; } }
public override bool MultipleSchema { get { return false; } }
public override string EngineName { get { return "SQLite"; } }

internal override IDbConnection CreateConnection() {
return new MCGSQLiteConnection();
Expand All @@ -43,9 +45,26 @@ public sealed class SQLiteBackend : IDatabaseBackend {

internal override IDbDataParameter CreateParameter() {
return new SQLiteParameter();
}


public override void LoadDependencies() {
// on macOS/Linux, use the system provided sqlite3 native library
if (!IOperatingSystem.DetectOS().IsWindows) return;

Server.CheckFile("sqlite3_x32.dll");
Server.CheckFile("sqlite3_x64.dll");

// sqlite3.dll is the .DLL that MCGalaxy will actually load on Windows
try {
string dll = IntPtr.Size == 8 ? "sqlite3_x64.dll" : "sqlite3_x32.dll";
if (File.Exists(dll)) File.Copy(dll, "sqlite3.dll", true);
} catch (Exception ex) {
// e.g. can happen when multiple server instances running
Logger.LogError("Error moving SQLite dll", ex);
}
}



public override void CreateDatabase() { }

public override string RawGetDateTime(IDataRecord record, int col) {
Expand Down
3 changes: 3 additions & 0 deletions MCGalaxy/Database/IDatabaseBackend.cs
Expand Up @@ -30,6 +30,7 @@ public abstract class IDatabaseBackend
public abstract bool EnforcesTextLength { get; }
/// <summary> Whether this backend supports multiple database schemas. </summary>
public abstract bool MultipleSchema { get; }
public abstract string EngineName { get; }

internal abstract IDbConnection CreateConnection();
internal abstract IDbCommand CreateCommand(string sql, IDbConnection conn);
Expand All @@ -41,6 +42,8 @@ public abstract class IDatabaseBackend
public string CaselessLikeSuffix { get; protected set; }


/// <summary> Downloads and/or moves required DLLs </summary>
public abstract void LoadDependencies();
/// <summary> Creates the schema for this database (if required). </summary>
public abstract void CreateDatabase();

Expand Down
5 changes: 2 additions & 3 deletions MCGalaxy/Database/PlayerData.cs
Expand Up @@ -62,12 +62,12 @@ public class PlayerData {
"totalDeaths, Money, totalBlocks, totalKicked, Messages, TimeSpent",
p.name, p.ip, now, now, 1, "", 0, 0, 0, 0, 0, (long)p.TotalTime.TotalSeconds);

int id = -100000;
int id = -200;
Database.ReadRows("Players", "ID",
record => id = record.GetInt32(0),
"WHERE Name=@0", p.name);

if (id >= 0) {
if (id != -200) {
p.DatabaseID = id;
} else {
p.DatabaseID = NameConverter.InvalidNameID(p.name);
Expand Down Expand Up @@ -136,7 +136,6 @@ record => id = record.GetInt32(0),
data.TotalDeleted = UnpackHi(drawn);
return data;
}
internal static object Read(IDataRecord record, object arg) { return Parse(record); }

internal static long ParseLong(string value) {
return (value.Length == 0 || value.CaselessEq("null")) ? 0 : long.Parse(value);
Expand Down
9 changes: 9 additions & 0 deletions MCGalaxy/Server/OperatingSystem.cs
Expand Up @@ -35,9 +35,16 @@ public abstract class IOperatingSystem
/// <remarks> Does not return when restart is successful
/// (since current process image is replaced) </remarks>
public abstract void RestartProcess();
public abstract bool IsWindows { get; }


static IOperatingSystem detectedOS;
public unsafe static IOperatingSystem DetectOS() {
detectedOS = detectedOS ?? DoDetectOS();
return detectedOS;
}

unsafe static IOperatingSystem DoDetectOS() {
PlatformID platform = Environment.OSVersion.Platform;
if (platform == PlatformID.Win32NT || platform == PlatformID.Win32Windows)
return new WindowsOS();
Expand Down Expand Up @@ -71,6 +78,7 @@ class WindowsOS : IOperatingSystem
static extern int GetSystemTimes(out ulong idleTime, out ulong kernelTime, out ulong userTime);

public override void RestartProcess() { }
public override bool IsWindows { get { return true; } }
}

class macOS : UnixOS
Expand Down Expand Up @@ -134,6 +142,7 @@ class UnixOS : IOperatingSystem
public override void RestartProcess() {
if (Server.CLIMode) HACK_Execvp();
}
public override bool IsWindows { get { return false; } }

#if !NETSTANDARD
[DllImport("libc", SetLastError = true)]
Expand Down
3 changes: 2 additions & 1 deletion MCGalaxy/Server/Server.DB.cs
Expand Up @@ -53,7 +53,8 @@ public sealed partial class Server {

static void InitDatabase() {
if (!Directory.Exists("blockdb")) Directory.CreateDirectory("blockdb");


Logger.Log(LogType.SystemActivity, "Using {0} for database backend", Database.Backend.EngineName);
try {
Database.Backend.CreateDatabase();
} catch (Exception e) {
Expand Down
23 changes: 6 additions & 17 deletions MCGalaxy/Server/Server.cs
Expand Up @@ -20,7 +20,6 @@
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
Expand All @@ -33,6 +32,7 @@
using MCGalaxy.Games;
using MCGalaxy.Network;
using MCGalaxy.Scripting;
using MCGalaxy.SQL;
using MCGalaxy.Tasks;
using MCGalaxy.Util;
using MCGalaxy.Modules.Awards;
Expand Down Expand Up @@ -60,7 +60,7 @@ public sealed partial class Server
Logger.Log(type, message);
}

static void CheckFile(string file) {
public static void CheckFile(string file) {
if (File.Exists(file)) return;

Logger.Log(LogType.SystemActivity, file + " doesn't exist, Downloading..");
Expand Down Expand Up @@ -91,14 +91,12 @@ public sealed partial class Server
shuttingDown = false;
Logger.Log(LogType.SystemActivity, "Starting Server");
ServicePointManager.Expect100Continue = false;
ForceEnableTLS();

CheckFile("MySql.Data.dll");
CheckFile("sqlite3_x32.dll");
CheckFile("sqlite3_x64.dll");
ForceEnableTLS();

SQLiteBackend.Instance.LoadDependencies();
CheckFile("MySql.Data.dll"); // TODO move to MySQL backend ?

EnsureFilesExist();
MoveSqliteDll();
MoveOutdatedFiles();

LoadAllSettings();
Expand Down Expand Up @@ -128,15 +126,6 @@ public sealed partial class Server
try { ServicePointManager.SecurityProtocol |= (SecurityProtocolType)0xC00; } catch { }
}

static void MoveSqliteDll() {
try {
string dll = IntPtr.Size == 8 ? "sqlite3_x64.dll" : "sqlite3_x32.dll";
if (File.Exists(dll)) File.Copy(dll, "sqlite3.dll", true);
} catch (Exception ex) {
Logger.LogError("Error moving SQLite dll", ex);
}
}

static void EnsureFilesExist() {
EnsureDirectoryExists("properties");
EnsureDirectoryExists("levels");
Expand Down

0 comments on commit 1fdc083

Please sign in to comment.