Skip to content

Commit

Permalink
fix: don't blow up on CooldownGroup null/DbGuidId missing (#1965)
Browse files Browse the repository at this point in the history
* feat: automatic database migration via command line flag

missed migration string

* fix game database migration for sqlite

* fixed player db migration
- added more logging
- made it easier to debug
  • Loading branch information
lodicolo authored Oct 15, 2023
1 parent 361d63d commit 0f3aa04
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 134 deletions.
2 changes: 1 addition & 1 deletion Intersect (Core)/GameObjects/ItemBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public AnimationBase EquipmentAnimation
/// <summary>
/// Defines which cooldown group this item belongs to.
/// </summary>
public string CooldownGroup { get; set; } = string.Empty;
public string? CooldownGroup { get; set; }

/// <summary>
/// Configures whether this should not trigger and be triggered by the global cooldown.
Expand Down
101 changes: 43 additions & 58 deletions Intersect.Server/Core/ServerCommandLineOptions.cs
Original file line number Diff line number Diff line change
@@ -1,73 +1,58 @@
using System.Collections.Generic;
using System.IO;
using System.Linq;

using CommandLine;

using CommandLine;
using Intersect.Core;
using Intersect.Network;

namespace Intersect.Server.Core
{
namespace Intersect.Server.Core;

internal partial struct ServerCommandLineOptions : ICommandLineOptions
internal struct ServerCommandLineOptions : ICommandLineOptions
{
public ServerCommandLineOptions(
bool doNotShowConsole,
bool doNotHaltOnError,
bool migrateAutomatically,
bool noNatPunchthrough,
bool noNetworkCheck,
ushort port,
string workingDirectory,
IEnumerable<string> pluginDirectories
)
{
DoNotShowConsole = doNotShowConsole;
DoNotHaltOnError = doNotHaltOnError;
MigrateAutomatically = migrateAutomatically;
NoNatPunchthrough = noNatPunchthrough;
NoNetworkCheck = noNetworkCheck;
Port = port;
WorkingDirectory = workingDirectory;
PluginDirectories = pluginDirectories?.Select(Path.GetFullPath).ToArray();
}

public ServerCommandLineOptions(
bool doNotShowConsole,
bool doNotHaltOnError,
bool noNatPunchthrough,
bool noNetworkCheck,
ushort port,
ushort apiport,
string workingDirectory,
IEnumerable<string> pluginDirectories
)
{
DoNotShowConsole = doNotShowConsole;
DoNotHaltOnError = doNotHaltOnError;
NoNatPunchthrough = noNatPunchthrough;
NoNetworkCheck = noNetworkCheck;
Port = port;
ApiPort = apiport;
WorkingDirectory = workingDirectory;
PluginDirectories = pluginDirectories?.Select(Path.GetFullPath).ToArray();
}

[Option('C', "no-console", Default = false, Required = false)]
public bool DoNotShowConsole { get; }

[Option('H', "no-halt", Default = false, Required = false)]
public bool DoNotHaltOnError { get; }

[Option('U', "no-upnp", Default = false, Required = false)]
public bool NoNatPunchthrough { get; }
[Option('C', "no-console", Default = false, Required = false)]
public bool DoNotShowConsole { get; }

[Option('P', "no-port-check", Default = false, Required = false)]
public bool NoNetworkCheck { get; }
[Option('H', "no-halt", Default = false, Required = false)]
public bool DoNotHaltOnError { get; }

[Option('p', "port", Default = (ushort) 0, Required = false)]
public ushort Port { get; }
[Option('m', "migrate-automatically", Default = false, Required = false)]
public bool MigrateAutomatically { get; }

[Option('a', "apiport", Default = (ushort) 0, Required = false)]
public ushort ApiPort { get; }
[Option('U', "no-upnp", Default = false, Required = false)]
public bool NoNatPunchthrough { get; }

[Option("working-directory", Default = null, Required = false)]
public string WorkingDirectory { get; }
[Option('P', "no-port-check", Default = false, Required = false)]
public bool NoNetworkCheck { get; }

[Option('p', "plugin-directory", Default = null, Required = false)]
public IEnumerable<string> PluginDirectories { get; }
[Option('p', "port", Default = (ushort)0, Required = false)]
public ushort Port { get; }

public ushort ValidPort(ushort defaultPort)
{
return PortHelper.IsValidPort(Port) ? Port : defaultPort;
}
[Option("working-directory", Default = null, Required = false)]
public string WorkingDirectory { get; }

public ushort ValidApiPort(ushort defaultApiPort)
{
return PortHelper.IsValidPort(defaultApiPort) ? ApiPort : defaultApiPort;
}
[Option('p', "plugin-directory", Default = null, Required = false)]
public IEnumerable<string> PluginDirectories { get; }

public ushort ValidPort(ushort defaultPort)
{
return PortHelper.IsValidPort(Port) ? Port : defaultPort;
}

}
}
63 changes: 37 additions & 26 deletions Intersect.Server/Database/DbInterface.cs
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private static void ProcessMigrations<TContext>(TContext context)
context.MigrationsProcessed(processedMigrations.ToArray());
}

private static bool EnsureUpdated()
private static bool EnsureUpdated(IServerContext serverContext)
{
Log.Verbose("Creating game context...");
using var gameContext = GameContext.Create(new DatabaseContextOptions
Expand All @@ -273,6 +273,7 @@ private static bool EnsureUpdated()
DatabaseType = Options.Instance.GameDatabase.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(),
});

Log.Verbose("Creating player context...");
Expand All @@ -285,6 +286,7 @@ private static bool EnsureUpdated()
DatabaseType = Options.Instance.PlayerDatabase.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(),
});

Log.Verbose("Creating logging context...");
Expand All @@ -297,6 +299,7 @@ private static bool EnsureUpdated()
DatabaseType = Options.Instance.LoggingDatabase.Type,
EnableDetailedErrors = true,
EnableSensitiveDataLogging = true,
LoggerFactory = new IntersectLoggerFactory(),
});

// We don't want anyone running the old migration tool accidentally
Expand Down Expand Up @@ -338,36 +341,44 @@ private static bool EnsureUpdated()

if (showMigrationWarning)
{
Console.WriteLine();
Console.WriteLine(Strings.Database.upgraderequired);
Console.WriteLine(
Strings.Database.upgradebackup.ToString(Strings.Database.upgradeready, Strings.Database.upgradeexit)
);

Console.WriteLine();
while (true)
if (serverContext.StartupOptions.MigrateAutomatically)
{
Console.Write("> ");
var input = Console.ReadLine().Trim();
if (input == Strings.Database.upgradeready.ToString().Trim())
Console.WriteLine(Strings.Database.MigratingAutomatically);
Log.Default.Write("Skipping user prompt for database migration...");
}
else
{
Console.WriteLine();
Console.WriteLine(Strings.Database.upgraderequired);
Console.WriteLine(
Strings.Database.upgradebackup.ToString(Strings.Database.upgradeready, Strings.Database.upgradeexit)
);

Console.WriteLine();
while (true)
{
break;
}
Console.Write("> ");
var input = Console.ReadLine().Trim();
if (input == Strings.Database.upgradeready.ToString().Trim())
{
break;
}

if (
!string.Equals(
input,
Strings.Database.upgradeexit.ToString().Trim(),
StringComparison.CurrentCultureIgnoreCase
if (
!string.Equals(
input,
Strings.Database.upgradeexit.ToString().Trim(),
StringComparison.CurrentCultureIgnoreCase
)
)
)
{
continue;
}
{
continue;
}

Environment.Exit(1);
Environment.Exit(1);

return false;
return false;
}
}

Console.WriteLine();
Expand Down Expand Up @@ -395,7 +406,7 @@ internal static bool InitDatabase(IServerContext serverContext)
{
Console.WriteLine("Initializing database...");

if (!EnsureUpdated())
if (!EnsureUpdated(serverContext))
{
Console.Error.WriteLine("Database not updated.");
return false;
Expand Down
Loading

0 comments on commit 0f3aa04

Please sign in to comment.