Tutorial backend, deletion of Nancy

I'll probably do a SaaS webadmin tool for the MUD in ASP.NET someday.
This commit is contained in:
Michael 2017-01-17 17:26:27 -05:00
parent b348a76db7
commit 18f93056b7
12 changed files with 77 additions and 257 deletions

View file

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true"/>
<loadFromRemoteSources enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true"/>
<loadFromRemoteSources enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -33,119 +33,10 @@ using System.IO;
using Newtonsoft.Json;
using System.Net;
using System.Net.Sockets;
using Nancy.Hosting.Self;
using Nancy;
using Nancy.Authentication.Basic;
using Nancy.Security;
using Nancy.TinyIoc;
using Nancy.Bootstrapper;
namespace ShiftOS.Server
{
/// <summary>
/// user mapper.
/// </summary>
public interface IUserMapper
{
/// <summary>
/// Get the real username from an identifier
/// </summary>
/// <param name="identifier">User identifier</param>
/// <param name="context">The current NancyFx context</param>
/// <returns>Matching populated IUserIdentity object, or empty</returns>
IUserIdentity GetUserFromIdentifier(Guid identifier, NancyContext context);
}
/// <summary>
/// MUD user validator.
/// </summary>
public class MUDUserValidator : IUserValidator
{
/// <summary>
/// Validate the specified username and password.
/// </summary>
/// <param name="username">Username.</param>
/// <param name="password">Password.</param>
public IUserIdentity Validate(string username, string password)
{
if(username == Program.AdminUsername && password == Program.AdminPassword)
{
return null;
}
else
{
return null;
}
}
}
/// <summary>
/// MUD user identity.
/// </summary>
public class MUDUserIdentity : IUserIdentity
{
/// <summary>
/// The claims of the authenticated user.
/// </summary>
/// <value>The claims.</value>
public IEnumerable<string> Claims
{
get
{
return null;
}
}
/// <summary>
/// The username of the authenticated user.
/// </summary>
/// <value>The name of the user.</value>
public string UserName
{
get
{
return uname;
}
}
/// <summary>
/// The uname.
/// </summary>
public string uname = "";
/// <summary>
/// Initializes a new instance of the <see cref="ShiftOS.Server.MUDUserIdentity"/> class.
/// </summary>
/// <param name="username">Username.</param>
public MUDUserIdentity(string username)
{
uname = username;
}
}
/// <summary>
/// Authentication bootstrapper.
/// </summary>
public class AuthenticationBootstrapper : DefaultNancyBootstrapper
{
/// <summary>
/// Initialise the bootstrapper - can be used for adding pre/post hooks and
/// any other initialisation tasks that aren't specifically container setup
/// related
/// </summary>
/// <param name="container">Container instance for resolving types if required.</param>
/// <returns>The startup.</returns>
/// <param name="pipelines">Pipelines.</param>
protected override void ApplicationStartup(TinyIoCContainer container, IPipelines pipelines)
{
base.ApplicationStartup(container, pipelines);
pipelines.EnableBasicAuthentication(new BasicAuthenticationConfiguration(
container.Resolve<MUDUserValidator>(),
"MUD", UserPromptBehaviour.NonAjax));
}
}
/// <summary>
/// Program.
/// </summary>
@ -294,17 +185,11 @@ namespace ShiftOS.Server
}
var hConf = new HostConfiguration();
hConf.UrlReservations.CreateAutomatically = true;
var nancy = new NancyHost(hConf, new Uri("http://localhost:13371/"));
server.OnStopped += (o, a) =>
{
nancy.Stop();
};
Console.WriteLine("Server stopping.");
nancy.Start();
};
}
/// <summary>

View file

@ -37,18 +37,6 @@
<HintPath>..\packages\DynamicLua.1.1.2.0\lib\net40-Client\DynamicLua.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.1.4.1\lib\net40\Nancy.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Authentication.Basic, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Authentication.Basic.1.4.1\lib\net40\Nancy.Authentication.Basic.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Nancy.Hosting.Self, Version=1.4.1.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Nancy.Hosting.Self.1.4.1\lib\net40\Nancy.Hosting.Self.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NetSockets">
<HintPath>..\Libraries\NetSockets.dll</HintPath>
</Reference>
@ -73,7 +61,6 @@
<DesignTime>True</DesignTime>
<DependentUpon>Resources.resx</DependentUpon>
</Compile>
<Compile Include="WebAdmin.cs" />
</ItemGroup>
<ItemGroup>
<None Include="App.config" />

View file

@ -1,115 +0,0 @@
/*
* MIT License
*
* Copyright (c) 2017 Michael VanOverbeek and ShiftOS devs
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using Nancy;
using Nancy.Security;
using NetSockets;
using Newtonsoft.Json;
using ShiftOS.Objects;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Server
{
public class WebAdmin : NancyModule
{
private Guid thisGuid { get; set; }
public WebAdmin()
{
this.RequiresAuthentication();
client = new NetObjectClient();
client.OnReceived += (o, a) =>
{
var msg = a.Data.Object as ServerMessage;
if (msg.Name == "Welcome")
{
thisGuid = new Guid(msg.Contents);
}
};
client.Connect(Program.server.Address.MapToIPv4().ToString(), 13370);
string template = Properties.Resources.Home;
Get["/"] = _ => { return GetPage(template, "index.html"); };
Get["/{page}"] = parameters =>
{
return GetPage(template, parameters.page);
};
}
public NetObjectClient client = new NetObjectClient();
public string GetPage(string template, string page)
{
string pageContents = File.ReadAllText("adm/" + page);
string page_text = template.Replace("{BODY}", pageContents);
page_text = page_text.Replace("{IP_ADDR}", client.RemoteHost.ToString());
page_text = page_text.Replace("{PORT}", client.RemotePort.ToString());
return page_text;
}
public string GrabResource(string page)
{
var type = this.GetType();
foreach(var property in type.GetProperties(BindingFlags.Public | BindingFlags.Instance))
{
foreach(var attr in property.GetCustomAttributes(false))
{
if(attr is PageAttribute)
{
if(page == (attr as PageAttribute).Name)
{
return property.GetGetMethod().Invoke(this, null) as string;
}
}
}
}
return Properties.Resources.NotFound;
}
}
public class PageAttribute :Attribute
{
public PageAttribute(string name)
{
Name = name;
}
public string Name { get; set; }
}
}

View file

@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="DynamicLua" version="1.1.2.0" targetFramework="net452" />
<package id="Nancy" version="1.4.1" targetFramework="net452" />
<package id="Nancy.Authentication.Basic" version="1.4.1" targetFramework="net452" />
<package id="Nancy.Hosting.Self" version="1.4.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="9.0.1" targetFramework="net452" />
</packages>

View file

@ -4,7 +4,7 @@
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true"/>
<loadFromRemoteSources enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />

View file

@ -173,6 +173,7 @@ namespace ShiftOS.WinForms
Thread.Sleep(500);
TextType("I will reboot your system in Tutorial Mode now. Complete the tutorial, and you shall be on your way.");
SaveSystem.CurrentSave = MySave;
SaveSystem.CurrentSave.StoryPosition = 1;
SaveSystem.SaveGame();
this.Invoke(new Action(() =>
{

View file

@ -1,9 +1,15 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
</startup>
<runtime>
<loadFromRemoteSources enabled="true"/>
<loadFromRemoteSources enabled="true" />
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-9.0.0.0" newVersion="9.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View file

@ -149,11 +149,24 @@ namespace ShiftOS.Engine
Shiftorium.Init();
while (CurrentSave.StoryPosition < 5)
while (CurrentSave.StoryPosition < 1)
{
}
if(CurrentSave.StoryPosition == 1)
{
Desktop.InvokeOnWorkerThread(new Action(() =>
{
TutorialManager.StartTutorial();
}));
while(CurrentSave.StoryPosition < 2)
{
}
}
Thread.Sleep(75);

View file

@ -163,6 +163,7 @@
<Compile Include="Story.cs" />
<Compile Include="TerminalBackend.cs" />
<Compile Include="TerminalTextWriter.cs" />
<Compile Include="TutorialManager.cs" />
<Compile Include="VirusEngine.cs" />
<Compile Include="WinOpenAttribute.cs" />
<EmbeddedResource Include="AltTabWindow.resx">

View file

@ -0,0 +1,33 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ShiftOS.Engine
{
public static class TutorialManager
{
private static ITutorial _tut = null;
public static void RegisterTutorial(ITutorial tut)
{
_tut = tut;
_tut.OnComplete += (o, a) =>
{
SaveSystem.CurrentSave.StoryPosition = 2;
};
}
public static void StartTutorial()
{
_tut.Start();
}
}
public interface ITutorial
{
void Start();
event EventHandler OnComplete;
}
}