Add lightweight JSON parsing library based off 'simple-json'.

This commit is contained in:
UnknownShadow200 2015-11-03 19:13:41 +11:00
parent 928ab17ec2
commit 29db4b88cf
5 changed files with 2619 additions and 0 deletions

253
ClassicalSharp.csproj Normal file
View File

@ -0,0 +1,253 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<ProjectGuid>{BEB1C785-5CAD-48FF-A886-876BF0A318D4}</ProjectGuid>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<OutputType>Exe</OutputType>
<RootNamespace>ClassicalSharp</RootNamespace>
<AssemblyName>ClassicalSharp</AssemblyName>
<TargetFrameworkVersion>v2.0</TargetFrameworkVersion>
<AppDesignerFolder>Properties</AppDesignerFolder>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
<NoStdLib>False</NoStdLib>
<WarningLevel>4</WarningLevel>
<TreatWarningsAsErrors>False</TreatWarningsAsErrors>
<TargetFrameworkProfile />
<FileUpgradeFlags>
</FileUpgradeFlags>
<UpgradeBackupLocation>
</UpgradeBackupLocation>
<OldToolsVersion>3.5</OldToolsVersion>
<PublishUrl>publish\</PublishUrl>
<Install>true</Install>
<InstallFrom>Disk</InstallFrom>
<UpdateEnabled>false</UpdateEnabled>
<UpdateMode>Foreground</UpdateMode>
<UpdateInterval>7</UpdateInterval>
<UpdateIntervalUnits>Days</UpdateIntervalUnits>
<UpdatePeriodically>false</UpdatePeriodically>
<UpdateRequired>false</UpdateRequired>
<MapFileExtensions>true</MapFileExtensions>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>1.0.0.%2a</ApplicationVersion>
<IsWebBootstrapper>false</IsWebBootstrapper>
<UseApplicationTrust>false</UseApplicationTrust>
<BootstrapperEnabled>true</BootstrapperEnabled>
<RunCodeAnalysis>False</RunCodeAnalysis>
<NoWin32Manifest>False</NoWin32Manifest>
<IntermediateOutputPath>obj\$(Configuration)\</IntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Platform)' == 'AnyCPU' ">
<PlatformTarget>x86</PlatformTarget>
<RegisterForComInterop>False</RegisterForComInterop>
<GenerateSerializationAssemblies>Auto</GenerateSerializationAssemblies>
<BaseAddress>4194304</BaseAddress>
<FileAlignment>4096</FileAlignment>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<OutputPath>output\debug\</OutputPath>
<DebugSymbols>true</DebugSymbols>
<DebugType>Full</DebugType>
<Optimize>False</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<StartAction>Project</StartAction>
<StartArguments>wwwf 127.0.0.1 25565</StartArguments>
<BaseIntermediateOutputPath>obj\</BaseIntermediateOutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">
<OutputPath>output\release\</OutputPath>
<DebugSymbols>false</DebugSymbols>
<DebugType>None</DebugType>
<Optimize>True</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TRACE</DefineConstants>
</PropertyGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.Targets" />
<ItemGroup>
<Reference Include="Ionic.Zip.Reduced">
<HintPath>Ionic.Zip.Reduced.dll</HintPath>
</Reference>
<Reference Include="OpenTK">
<HintPath>OpenTK.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
</ItemGroup>
<ItemGroup>
<Compile Include="2D\DrawTextArgs.cs" />
<Compile Include="2D\Screens\ChatScreen.cs" />
<Compile Include="2D\Screens\ErrorScreen.cs" />
<Compile Include="2D\Screens\FpsScreen.cs" />
<Compile Include="2D\Screens\InventoryScreen.cs" />
<Compile Include="2D\Screens\LoadingMapScreen.cs" />
<Compile Include="2D\Screens\NormalScreen.cs" />
<Compile Include="2D\Screens\PauseScreen.cs" />
<Compile Include="2D\Screens\Screen.cs" />
<Compile Include="2D\Utils2D.cs" />
<Compile Include="2D\Widgets\HotbarWidget.cs" />
<Compile Include="2D\Widgets\ExtPlayerListWidget.cs" />
<Compile Include="2D\Widgets\SlotWidget.cs" />
<Compile Include="2D\Widgets\TextGroupWidget.cs" />
<Compile Include="2D\Widgets\TextInputWidget.cs" />
<Compile Include="2D\Widgets\TextWidget.cs" />
<Compile Include="2D\Widgets\Widget.cs" />
<Compile Include="Blocks\BlockId.cs" />
<Compile Include="Blocks\BlockInfo.Biomes.cs" />
<Compile Include="Blocks\BlockInfo.cs" />
<Compile Include="Blocks\BlockInfo.Culling.cs" />
<Compile Include="Blocks\BlockInfo.Optimised.cs" />
<Compile Include="Blocks\Model\BedModel.cs" />
<Compile Include="Blocks\Model\BiomeColouredModel.cs" />
<Compile Include="Blocks\Model\CactusModel.cs" />
<Compile Include="Blocks\Model\CubeModel.cs" />
<Compile Include="Blocks\Model\FenceModel.cs" />
<Compile Include="Blocks\Model\FluidModel.cs" />
<Compile Include="Blocks\Model\GrassCubeModel.cs" />
<Compile Include="Blocks\Model\IBlockModel.cs" />
<Compile Include="Blocks\Model\LeverModel.cs" />
<Compile Include="Blocks\Model\LogModel.cs" />
<Compile Include="Blocks\Model\RailsModel.cs" />
<Compile Include="Blocks\Model\SaplingModel.cs" />
<Compile Include="Blocks\Model\SeedsModel.cs" />
<Compile Include="Blocks\Model\SpriteModel.cs" />
<Compile Include="Blocks\Model\TorchModel.cs" />
<Compile Include="Blocks\Model\VariableHeightCubeModel.cs" />
<Compile Include="Entities\Entity.cs" />
<Compile Include="Entities\EntityManager.cs" />
<Compile Include="Entities\EntityMetadata.cs" />
<Compile Include="Entities\LivingEntity.cs" />
<Compile Include="Entities\LocalPlayer.cs" />
<Compile Include="Entities\NetPlayer.cs" />
<Compile Include="Entities\NullEntity.cs" />
<Compile Include="Entities\Objects\FallingBlockEntity.cs" />
<Compile Include="Entities\Objects\ObjectEntity.cs" />
<Compile Include="Entities\Particle.cs" />
<Compile Include="Entities\ParticleManager.cs" />
<Compile Include="Entities\Player.cs" />
<Compile Include="Game\Game.Chat.cs" />
<Compile Include="Game\Game.cs" />
<Compile Include="Game\Game.Events.cs" />
<Compile Include="Game\Game.InputHandling.cs" />
<Compile Include="GraphicsAPI\IGraphicsApi.cs" />
<Compile Include="GraphicsAPI\OpenGLApi.cs" />
<Compile Include="Camera.cs" />
<Compile Include="Commands\Command.cs" />
<Compile Include="Commands\DefaultCommands.cs" />
<Compile Include="GraphicsAPI\VertexFormats.cs" />
<Compile Include="Items\ItemId.cs" />
<Compile Include="Items\ItemsInfo.cs" />
<Compile Include="Items\Items\AxeItem.cs" />
<Compile Include="Items\Items\Item.cs" />
<Compile Include="Items\Items\PickaxeItem.cs" />
<Compile Include="Items\Items\ShearsItem.cs" />
<Compile Include="Items\Items\ShovelItem.cs" />
<Compile Include="Items\Items\SwordItem.cs" />
<Compile Include="Items\Items\ToolItem.cs" />
<Compile Include="MeshBuilders\ChunkMeshBuilder.ChunkReading.cs" />
<Compile Include="MeshBuilders\ChunkMeshBuilder.cs" />
<Compile Include="MeshBuilders\JSON.cs" />
<Compile Include="Model\BlockModel.cs" />
<Compile Include="Model\ChickenModel.cs" />
<Compile Include="Model\CreeperModel.cs" />
<Compile Include="Model\IModel.cs" />
<Compile Include="Model\ModelCache.cs" />
<Compile Include="Model\ModelPart.cs" />
<Compile Include="Model\PigModel.cs" />
<Compile Include="Model\PlayerModel.cs" />
<Compile Include="Model\SheepModel.cs" />
<Compile Include="Model\SkeletonModel.cs" />
<Compile Include="Model\SpiderModel.cs" />
<Compile Include="Model\ZombieModel.cs" />
<Compile Include="Network\Enums.cs" />
<Compile Include="Network\NetReader.cs" />
<Compile Include="Network\NetworkProcessor.cs" />
<Compile Include="Network\AsyncDownloader.cs" />
<Compile Include="Commands\CommandManager.cs" />
<Compile Include="Commands\CommandReader.cs" />
<Compile Include="Network\NetworkProcessor.IO.cs" />
<Compile Include="Network\NetWriter.cs" />
<Compile Include="Network\Packets\InboundPackets.cs" />
<Compile Include="Network\Packets\OutboundPackets.cs" />
<Compile Include="Network\Packets\Packet.cs" />
<Compile Include="Physics\BoundingBox.cs" />
<Compile Include="Physics\Entity.Physics.cs" />
<Compile Include="Physics\IntersectionUtils.cs" />
<Compile Include="Physics\Picking.cs" />
<Compile Include="Program.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Rendering\LegacyEnvRenderer.cs" />
<Compile Include="Rendering\EnvRenderer.cs" />
<Compile Include="Rendering\LegacyFastEnvRenderer.cs" />
<Compile Include="Rendering\FrustumCulling.cs" />
<Compile Include="Rendering\MapRenderer.cs" />
<Compile Include="Rendering\NormalEnvRenderer.cs" />
<Compile Include="Rendering\PickingRenderer.cs" />
<Compile Include="Rendering\EntityRenderer.cs" />
<Compile Include="Util\ConcurrentQueue.cs" />
<Compile Include="Util\FastBitmap.cs" />
<Compile Include="Util\FastColour.cs" />
<Compile Include="Util\NibbleArray.cs" />
<Compile Include="Util\TextureAtlas2D.cs" />
<Compile Include="Util\Utils.cs" />
<Compile Include="Util\Vector2I.cs" />
<Compile Include="Util\Vector3I.cs" />
<Compile Include="Window\ChestWindow.cs" />
<Compile Include="Window\DispenserWindow.cs" />
<Compile Include="Window\FurnaceWindow.cs" />
<Compile Include="Window\InventoryWindow.cs" />
<Compile Include="Window\Slot.cs" />
<Compile Include="Window\Window.cs" />
<Compile Include="Window\WindowManager.cs" />
<Compile Include="Window\WorkbenchWindow.cs" />
<Compile Include="World\Chunk.cs" />
<Compile Include="World\ChunkPartialUpdate.cs" />
<Compile Include="World\Map.cs" />
</ItemGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0">
<Visible>False</Visible>
<ProductName>Microsoft .NET Framework 4 %28x86 and x64%29</ProductName>
<Install>true</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1 Client Profile</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Net.Framework.3.5.SP1">
<Visible>False</Visible>
<ProductName>.NET Framework 3.5 SP1</ProductName>
<Install>false</Install>
</BootstrapperPackage>
<BootstrapperPackage Include="Microsoft.Windows.Installer.3.1">
<Visible>False</Visible>
<ProductName>Windows Installer 3.1</ProductName>
<Install>true</Install>
</BootstrapperPackage>
</ItemGroup>
<ItemGroup>
<None Include="app.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="2D\Screens" />
<Folder Include="2D\Widgets" />
<Folder Include="Blocks" />
<Folder Include="Blocks\Model" />
<Folder Include="Entities\Objects" />
<Folder Include="GraphicsAPI" />
<Folder Include="Entities" />
<Folder Include="Items\Items" />
<Folder Include="MeshBuilders" />
<Folder Include="Game" />
<Folder Include="Model" />
<Folder Include="Network\Packets" />
<Folder Include="Items" />
<Folder Include="World" />
<Folder Include="Window" />
<Folder Include="Util" />
<Folder Include="Physics" />
</ItemGroup>
</Project>

View File

@ -75,6 +75,7 @@
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="Utils\Client.cs" />
<Compile Include="Utils\ClientStartData.cs" />
<Compile Include="Utils\JSON.cs" />
<Compile Include="Utils\Secure.cs" />
<Compile Include="WebService\ClassiCubeSession.cs" />
<Compile Include="WebService\IWebTask.cs" />

229
Launcher2/Utils/JSON.cs Normal file
View File

@ -0,0 +1,229 @@
//-----------------------------------------------------------------------
// <copyright file="SimpleJson.cs" company="The Outercurve Foundation">
// Copyright ( c ) 2011, The Outercurve Foundation.
//
// Licensed under the MIT License ( the "License" );
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.opensource.org/licenses/mit-license.php
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// </copyright>
// <author>Nathan Totten ( ntotten.com ), Jim Zimmerman ( jimzimmerman.com ) and Prabir Shrestha ( prabir.me )</author>
// <website>https://github.com/facebook-csharp-sdk/simple-json</website>
//-----------------------------------------------------------------------
// original json parsing code from http://techblog.procurios.nl/k/618/news/view/14605/14863/How-do-I-write-my-own-parser-for-JSON.html
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace SimpleJson {
public static class SimpleJson {
const int TOKEN_NONE = 0, TOKEN_CURLY_OPEN = 1, TOKEN_CURLY_CLOSE = 2;
const int TOKEN_SQUARED_OPEN = 3, TOKEN_SQUARED_CLOSE = 4, TOKEN_COLON = 5;
const int TOKEN_COMMA = 6, TOKEN_STRING = 7, TOKEN_NUMBER = 8;
const int TOKEN_TRUE = 9, TOKEN_FALSE = 10, TOKEN_NULL = 11;
static Dictionary<string, object> ParseObject( char[] json, ref int index, ref bool success ) {
Dictionary<string, object> table = new Dictionary<string, object>();
NextToken( json, ref index ); // skip {
while( true ) {
int token = LookAhead( json, index );
if( token == TOKEN_NONE ) {
success = false; return null;
} else if( token == TOKEN_COMMA ) {
NextToken( json, ref index );
} else if( token == TOKEN_CURLY_CLOSE ) {
NextToken( json, ref index );
return table;
} else {
string name = ParseString( json, ref index, ref success );
if( !success ) {
success = false; return null;
}
token = NextToken( json, ref index );
if( token != TOKEN_COLON ) {
success = false; return null;
}
object value = ParseValue( json, ref index, ref success );
if( !success ) {
success = false; return null;
}
table[name] = value;
}
}
}
static List<object> ParseArray( char[] json, ref int index, ref bool success ) {
List<object> array = new List<object>();
NextToken( json, ref index ); // [
while( true ) {
int token = LookAhead( json, index );
if( token == TOKEN_NONE ) {
success = false; return null;
} else if( token == TOKEN_COMMA ) {
NextToken( json, ref index );
} else if( token == TOKEN_SQUARED_CLOSE ) {
NextToken( json, ref index );
return array;
} else {
object value = ParseValue( json, ref index, ref success );
if( !success ) return null;
array.Add( value );
}
}
}
static object ParseValue( char[] json, ref int index, ref bool success ) {
switch ( LookAhead( json, index ) ) {
case TOKEN_STRING:
return ParseString( json, ref index, ref success );
case TOKEN_NUMBER:
return ParseNumber( json, ref index, ref success );
case TOKEN_CURLY_OPEN:
return ParseObject( json, ref index, ref success );
case TOKEN_SQUARED_OPEN:
return ParseArray( json, ref index, ref success );
case TOKEN_TRUE:
NextToken( json, ref index );
return true;
case TOKEN_FALSE:
NextToken( json, ref index );
return false;
case TOKEN_NULL:
NextToken( json, ref index );
return null;
case TOKEN_NONE:
break;
}
success = false; return null;
}
static string ParseString( char[] json, ref int index, ref bool success ) {
StringBuilder s = new StringBuilder( 400 );
EatWhitespace( json, ref index );
char c = json[index++]; // "
while( true ) {
if( index == json.Length )
break;
c = json[index++];
if( c == '"' ) {
return s.ToString();
} else if( c == '\\' ) {
if( index == json.Length )
break;
c = json[index++];
if( c == 'u' ) {
int remainingLength = json.Length - index;
if( remainingLength >= 4 ) {
// parse the 32 bit hex into an integer codepoint
uint codePoint;
if( !( success = UInt32.TryParse( new String( json, index, 4 ), NumberStyles.HexNumber, CultureInfo.InvariantCulture, out codePoint ) ) )
return "";
s.Append( new String( (char)codePoint, 1 ) );
index += 4; // skip 4 chars
} else {
break;
}
} else {
s.Append( '?' );
}
} else {
s.Append( c );
}
}
success = false; return null;
}
const StringComparison caseless = StringComparison.OrdinalIgnoreCase;
static object ParseNumber( char[] json, ref int index, ref bool success ) {
EatWhitespace( json, ref index );
int lastIndex = GetLastIndexOfNumber( json, index );
int charLength = (lastIndex - index) + 1;
string str = new String( json, index, charLength );
index = lastIndex + 1;
if( str.IndexOf( ".", caseless ) != -1 || str.IndexOf( "e", caseless ) != -1 ) {
double number;
success = double.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out number );
return number;
} else {
long number;
success = long.TryParse( str, NumberStyles.Any, CultureInfo.InvariantCulture, out number );
return number;
}
}
static int GetLastIndexOfNumber( char[] json, int index ) {
int lastIndex = index;
for( ; lastIndex < json.Length; lastIndex++ ) {
if( "0123456789+-.eE".IndexOf( json[lastIndex] ) == -1 )
break;
}
return lastIndex - 1;
}
static void EatWhitespace( char[] json, ref int index ) {
for( ; index < json.Length; index++ ) {
if( " \t\n\r\b\f".IndexOf( json[index] ) == -1 )
break;
}
}
static int LookAhead( char[] json, int index ) {
int saveIndex = index;
return NextToken( json, ref saveIndex );
}
static int NextToken( char[] json, ref int index ) {
EatWhitespace( json, ref index );
if( index == json.Length )
return TOKEN_NONE;
char c = json[index];
index++;
if( c == '{' ) return TOKEN_CURLY_OPEN;
if( c == '}' ) return TOKEN_CURLY_CLOSE;
if( c == '[' ) return TOKEN_SQUARED_OPEN;
if( c == ']' ) return TOKEN_SQUARED_CLOSE;
if( c == ',' ) return TOKEN_COMMA;
if( c == '"' ) return TOKEN_STRING;
if( c == ':' ) return TOKEN_COLON;
if( c == '-' || ('0' <= c && c <= '9') )
return TOKEN_NUMBER;
index--;
if( CompareConstant( json, ref index, "false" ) )
return TOKEN_FALSE;
if( CompareConstant( json, ref index, "true" ) )
return TOKEN_TRUE;
if( CompareConstant( json, ref index, "null" ) )
return TOKEN_NULL;
return TOKEN_NONE;
}
static bool CompareConstant( char[] json, ref int index, string value ) {
int remaining = json.Length - index;
if( remaining < value.Length ) return false;
for( int i = 0; i < value.Length; i++ ) {
if( json[index + i] != value[i] ) return false;
}
index += value.Length;
return true;
}
}
}

View File

@ -71,6 +71,15 @@ namespace Launcher2 {
return GetResponseLines( response );
}
protected string GetHtmlAll( string uri, string referer ) {
HttpWebResponse response = MakeRequest( uri, referer, null );
using( Stream stream = response.GetResponseStream() ) {
using( StreamReader reader = new StreamReader( stream ) ) {
return reader.ReadToEnd();
}
}
}
protected IEnumerable<string> GetResponseLines( HttpWebResponse response ) {
using( Stream stream = response.GetResponseStream() ) {
using( StreamReader reader = new StreamReader( stream ) ) {

2127
MeshBuilders/JSON.cs Normal file

File diff suppressed because it is too large Load Diff