Skip to content

Commit

Permalink
Initial Commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Piispanen committed Oct 20, 2013
0 parents commit 92b0cf7
Show file tree
Hide file tree
Showing 10 changed files with 309 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/MultiServerChat/obj
/MultiServerChat/bin
20 changes: 20 additions & 0 deletions MultiServerChat.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 11.00
# Visual Studio 2010
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MultiServerChat", "MultiServerChat\MultiServerChat.csproj", "{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
EndGlobal
76 changes: 76 additions & 0 deletions MultiServerChat/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Newtonsoft.Json;

namespace MultiServerChat
{
class ConfigFile
{
public string RestURL = "http://127.0.0.1:7878/msc";
public string Token = "abcdef";
public string ChatFormat = "[{0}]{2}{3}{4}: {5}";
/// <summary>
/// Reads a configuration file from a given path
/// </summary>
/// <param name="path">string path</param>
/// <returns>ConfigFile object</returns>
public static ConfigFile Read(string path)
{
if (!File.Exists(path))
return new ConfigFile();
using (var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return Read(fs);
}
}

/// <summary>
/// Reads the configuration file from a stream
/// </summary>
/// <param name="stream">stream</param>
/// <returns>ConfigFile object</returns>
public static ConfigFile Read(Stream stream)
{
using (var sr = new StreamReader(stream))
{
var cf = JsonConvert.DeserializeObject<ConfigFile>(sr.ReadToEnd());
if (ConfigRead != null)
ConfigRead(cf);
return cf;
}
}

/// <summary>
/// Writes the configuration to a given path
/// </summary>
/// <param name="path">string path - Location to put the config file</param>
public void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
Write(fs);
}
}

/// <summary>
/// Writes the configuration to a stream
/// </summary>
/// <param name="stream">stream</param>
public void Write(Stream stream)
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(stream))
{
sw.Write(str);
}
}

/// <summary>
/// On config read hook
/// </summary>
public static Action<ConfigFile> ConfigRead;
}
}
Binary file added MultiServerChat/DLLS/HttpServer.dll
Binary file not shown.
Binary file added MultiServerChat/DLLS/Newtonsoft.Json.dll
Binary file not shown.
Binary file added MultiServerChat/DLLS/TShockAPI.dll
Binary file not shown.
Binary file added MultiServerChat/DLLS/TerrariaServer.exe
Binary file not shown.
108 changes: 108 additions & 0 deletions MultiServerChat/MultiServerChat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using HttpServer;
using Newtonsoft.Json;
using Rests;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;

namespace MultiServerChat
{
[ApiVersion(1,14)]
public class MultiServerChat : TerrariaPlugin
{
ConfigFile Config = new ConfigFile();
private string savePath = "";
public MultiServerChat(Main game) : base(game)
{
savePath = Path.Combine(TShock.SavePath, "multiserverchat.json");
Config = ConfigFile.Read(savePath);
Config.Write(savePath);
}

public override void Initialize()
{
ServerApi.Hooks.ServerChat.Register(this, OnChat, 10);
TShock.RestApi.Register(new SecureRestCommand("/msc", RestChat, "msc.canchat"));
}

private object RestChat(RestVerbs verbs, IParameterCollection parameters, SecureRest.TokenData tokenData)
{
if (!string.IsNullOrWhiteSpace(parameters["message"]))
{
var bytes = Convert.FromBase64String(parameters["message"]);
var str = Encoding.UTF8.GetString(bytes);
var message = Message.FromJson(str);
TShock.Utils.Broadcast(message.Text, message.Red, message.Green, message.Blue);
}

return new RestObject();
}

private void OnChat(ServerChatEventArgs args)
{
var tsplr = TShock.Players[args.Who];
if (tsplr == null)
{
return;
}

if (args.Text.StartsWith("/") && args.Text.Length > 1)
{
}
else
{
if (!tsplr.Group.HasPermission(Permissions.canchat))
{
return;
}

if (tsplr.mute)
{
return;
}

var message = new Message()
{
Text =
String.Format(Config.ChatFormat, TShock.Config.ServerName, tsplr.Group.Name, tsplr.Group.Prefix, tsplr.Name, tsplr.Group.Suffix,
args.Text),
Red = tsplr.Group.R,
Green = tsplr.Group.G,
Blue = tsplr.Group.B
};

var bytes = Encoding.UTF8.GetBytes(message.ToString());
var base64 = Convert.ToBase64String(bytes);

var uri = String.Format("{0}?message={1}&token={2}", Config.RestURL, base64, Config.Token);

var request = (HttpWebRequest)WebRequest.Create(uri);
request.GetResponse();
}
}
}

public class Message
{
public string Text { get; set; }
public byte Red { get; set; }
public byte Green { get; set; }
public byte Blue { get; set; }

public override string ToString()
{
return JsonConvert.SerializeObject(this, Formatting.Indented);
}

public static Message FromJson(string js)
{
return JsonConvert.DeserializeObject<Message>(js);
}
}
}
67 changes: 67 additions & 0 deletions MultiServerChat/MultiServerChat.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{B12B3BE8-7AC1-4732-90C6-38EECF8C2EFE}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>MultiServerChat</RootNamespace>
<AssemblyName>MultiServerChat</AssemblyName>
<TargetFrameworkVersion>v4.0</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="HttpServer">
<HintPath>DLLS\HttpServer.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json">
<HintPath>DLLS\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
<Reference Include="TerrariaServer">
<HintPath>DLLS\TerrariaServer.exe</HintPath>
</Reference>
<Reference Include="TShockAPI">
<HintPath>DLLS\TShockAPI.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Config.cs" />
<Compile Include="MultiServerChat.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>
36 changes: 36 additions & 0 deletions MultiServerChat/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;

// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("MultiServerChat")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("MultiServerChat")]
[assembly: AssemblyCopyright("Copyright © 2013")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]

// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]

// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("2e6becb5-7745-4d96-b6a1-ed393a0f3aea")]

// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

0 comments on commit 92b0cf7

Please sign in to comment.