port from perforce

This commit is contained in:
2026-04-18 22:31:51 +02:00
commit 8d0ab5b7cc
8409 changed files with 3972376 additions and 0 deletions

View File

@@ -0,0 +1,83 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="3.5" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.21022</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{0F7D7168-08C1-45AE-AAE3-80506939D7E6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>Aiwaz.Common</RootNamespace>
<AssemblyName>Aiwaz.Common</AssemblyName>
<TargetFrameworkVersion>v3.5</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="Ionic.Zip, Version=1.8.4.29, Culture=neutral, PublicKeyToken=edbe51ad942a3f5c, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\Ionic.Zip.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml.Linq">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data.DataSetExtensions">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Animations\AnimateableAttribute.cs" />
<Compile Include="Animations\Animation.cs" />
<Compile Include="Animations\AnimationManager.cs" />
<Compile Include="Animations\CosineValueAnimation.cs" />
<Compile Include="Animations\Event.cs" />
<Compile Include="Animations\LinearValueAnimation.cs" />
<Compile Include="FileSystem.cs" />
<Compile Include="HiPerfTimer.cs" />
<Compile Include="Win32Console.cs" />
<Compile Include="Timeline.cs" />
<Compile Include="Animations\ValueAnimation.cs" />
<Compile Include="Pair.cs" />
<Compile Include="ObjectFactory.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Aiwaz.Contracts\Aiwaz.Contracts.csproj">
<Project>{DE4D6FE6-D1FB-41A1-8ABA-19635B8FFD7A}</Project>
<Name>Aiwaz.Contracts</Name>
</ProjectReference>
</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>

View File

@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common.Animations
{
[AttributeUsage(AttributeTargets.Property)]
public class Animateable : Attribute
{
}
}

View File

@@ -0,0 +1,16 @@
namespace Aiwaz.Common.Animations
{
abstract public class Animation
{
public double StartTime { get; set; }
public double EndTime { get; set; }
public string AnimationName;
public Animation()
{
AnimationName = "Erroneous";
}
public abstract void Animate(double time, uint userData);
}
}

View File

@@ -0,0 +1,164 @@
using System.Collections.Generic;
namespace Aiwaz.Common.Animations
{
struct AnimationInterval
{
public AnimationInterval(double argBegin, double argEnd)
{
begin = argBegin;
end = argEnd;
}
public double begin;
public double end;
}
class ForwardComparer : IComparer<AnimationInterval>
{
public int Compare(AnimationInterval a, AnimationInterval b)
{
return a.begin < b.begin ? -1 : (a.begin > b.begin ? 1 : 0);
}
}
class BackwardComparer : IComparer<AnimationInterval>
{
public int Compare(AnimationInterval a, AnimationInterval b)
{
return a.end > b.end ? -1 : (a.end > b.end ? 1 : 0);
}
}
public static class AnimationManager
{
private static SortedList<AnimationInterval, List<Pair<Animation, uint>>> forwardSortedAnimations = new SortedList<AnimationInterval, List<Pair<Animation, uint>>>(new ForwardComparer());
private static SortedList<AnimationInterval, List<Pair<Animation, uint>>> backwardSortedAnimations = new SortedList<AnimationInterval, List<Pair<Animation, uint>>>(new BackwardComparer());
private static SortedList<AnimationInterval, List<Pair<Animation, uint>>> realTimeAnimations = new SortedList<AnimationInterval, List<Pair<Animation, uint>>>(new ForwardComparer());
private static SortedList<double, List<Pair<Event, uint>>> demoTimeEvents = new SortedList<double, List<Pair<Event, uint>>>();
private static SortedList<double, List<Pair<Event, uint>>> realTimeEvents = new SortedList<double, List<Pair<Event, uint>>>();
private static double previousDemoTime;
private static double previousRealTime;
public static void AddDemoTimeAnimation(double argBeginTime, double argEndTime, Animation argAnimation, uint argUserData)
{
forwardSortedAnimations[new AnimationInterval(argBeginTime, argEndTime)].Add(new Pair<Animation, uint>(argAnimation, argUserData));
backwardSortedAnimations[new AnimationInterval(argBeginTime, argEndTime)].Add(new Pair<Animation, uint>(argAnimation, argUserData));
}
public static void AddRealTimeAnimation(double argBeginTime, double argEndTime, Animation argAnimation, uint argUserData)
{
realTimeAnimations[new AnimationInterval(argBeginTime, argEndTime)].Add(new Pair<Animation, uint>(argAnimation, argUserData));
}
public static void AddDemoTimeEvent(double argTime, Event argEvent, uint argUserData)
{
demoTimeEvents[argTime].Add(new Pair<Event, uint>(argEvent, argUserData));
}
public static void AddRealTimeEvent(double argTime, Event argEvent, uint argUserData)
{
realTimeEvents[argTime].Add(new Pair<Event, uint>(argEvent, argUserData));
}
public static void Animate()
{
double demoTime = Timeline.DemoTime;
double realTime = Timeline.RealTime;
// Gather all active animations, they are the ones that started before now and end after now.
// Do this for both the demo time and real time.
List<Pair<Pair<Animation, uint>, double>> lk_ActiveAnimations = new List<Pair<Pair<Animation, uint>, double>>();
// Demo time animations: Demo time can go backwards, so search from both directions
if (Timeline.SpeedAndDirection > 0)
{
foreach (var entry in forwardSortedAnimations)
{
if (entry.Key.begin <= demoTime && entry.Key.end >= demoTime)
{
foreach (var animation in entry.Value)
{
lk_ActiveAnimations.Add(new Pair<Pair<Animation, uint>, double>(animation, (demoTime - entry.Key.begin) / (entry.Key.end - entry.Key.begin)));
}
}
if (entry.Key.begin > demoTime)
break;
}
}
else if (Timeline.SpeedAndDirection < 0)
{
foreach (var entry in backwardSortedAnimations)
{
if (entry.Key.end >= demoTime && entry.Key.begin <= demoTime)
{
foreach (var animation in entry.Value)
{
lk_ActiveAnimations.Add(new Pair<Pair<Animation, uint>, double>(animation, (demoTime - entry.Key.begin) / (entry.Key.end - entry.Key.begin)));
}
}
if (entry.Key.end < demoTime)
break;
}
}
// Real time animations: The time that we know doesn't allow us to travel to the past, so just check into the future:
foreach (var entry in realTimeAnimations)
{
if (entry.Key.begin <= demoTime && entry.Key.end >= demoTime)
{
foreach (var animation in entry.Value)
{
lk_ActiveAnimations.Add(new Pair<Pair<Animation, uint>, double>(animation, (demoTime - entry.Key.begin) / (entry.Key.end - entry.Key.begin)));
}
}
if (entry.Key.begin > demoTime)
break;
}
// Now that we know what animations correspond to this time, animate them!
foreach (var anim in lk_ActiveAnimations)
{
anim.First.First.Animate(anim.Second, anim.First.Second);
}
// Fire all events that happened since last time:
foreach (var entry in demoTimeEvents)
{
double time = entry.Key;
foreach (var evt in entry.Value)
{
if ((Timeline.SpeedAndDirection > 0) && (time >= previousDemoTime && time <= demoTime))
{
evt.First.Do(evt.Second);
}
else if ((Timeline.SpeedAndDirection < 0) && (time <= previousDemoTime && time >= demoTime))
{
evt.First.Undo(evt.Second);
}
}
}
foreach (var entry in realTimeEvents)
{
double time = entry.Key;
foreach (var evt in entry.Value)
{
if (time >= previousDemoTime && time <= demoTime)
{
evt.First.Do(evt.Second);
}
}
}
previousDemoTime = demoTime;
previousRealTime = realTime;
}
}
}

View File

@@ -0,0 +1,22 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common.Animations
{
public class CosineValueAnimation : ValueAnimation
{
public CosineValueAnimation()
: base()
{
AnimationName = "Cosine";
}
protected override float Calculate(double NormalizedTime)
{
float Interpolant = (float)((1.0 - Math.Cos(NormalizedTime * Math.PI)) / 2.0);
return (StartValue * (1.0f - Interpolant) + EndValue * Interpolant);
}
}
}

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common.Animations
{
abstract public class Event
{
abstract public Action<uint> Do { get; }
abstract public Action<uint> Undo { get; }
}
}

View File

@@ -0,0 +1,21 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common.Animations
{
public class LinearValueAnimation : ValueAnimation
{
public LinearValueAnimation()
: base()
{
AnimationName = "Linear";
}
protected override float Calculate(double NormalizedTime)
{
return (float)(StartValue * (1.0 - NormalizedTime) + EndValue * NormalizedTime);
}
}
}

View File

@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common.Animations
{
abstract public class ValueAnimation : Animation
{
public float StartValue { get; set; }
public float EndValue { get; set; }
public Func<float> ValueGetter;
public Action<float> ValueSetter;
public Func<string> ValueNamer;
public float Value
{
get { return ValueGetter(); }
set { ValueSetter(value); }
}
public ValueAnimation()
: base()
{
AnimationName = "Erroneous (Value)";
}
public override void Animate(double time, uint userData)
{
if (time < StartTime || time > EndTime)
return;
double Time = (time - StartTime) / (EndTime - StartTime);
Value = Calculate(Time);
}
protected abstract float Calculate(double NormalizedTime);
}
}

View File

@@ -0,0 +1,79 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Ionic.Zip;
using System.IO;
namespace Aiwaz.Common
{
public class FileSystem : IDisposable
{
private ZipFile zipFile;
public FileSystem()
{
if (File.Exists("data.pak"))
{
zipFile = ZipFile.Read("data.pak");
}
}
~FileSystem()
{
this.Dispose();
}
public Stream Open(string argFileName)
{
if (File.Exists(argFileName))
{
Stream fileStream = new FileStream(argFileName, FileMode.Open);
if (fileStream != null)
return fileStream;
}
if (zipFile != null)
{
var file = zipFile[argFileName];
if (file != null)
{
Stream outputStream = new MemoryStream();
file.Extract(outputStream);
return outputStream;
}
}
throw new FileNotFoundException(argFileName);
}
#region IDisposable Members
public void Dispose()
{
if (zipFile != null)
{
zipFile.Dispose();
}
}
#endregion
}
public static class BinaryReaderExtensions
{
public static string ReadNullTerminatedString(this BinaryReader reader)
{
string result = string.Empty;
char readChar = '\0';
do
{
readChar = reader.ReadChar();
if (readChar != '\0')
result += readChar;
}
while (readChar != '\0');
return result;
}
}
}

View File

@@ -0,0 +1,44 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Aiwaz.Common
{
public static class HiPerfTimer
{
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceCounter(out long performanceCount);
[DllImport("Kernel32.dll")]
private static extern bool QueryPerformanceFrequency(out long frequency);
private static double? startTime;
private static double lastTime;
public static double LastDeltaTime
{
get;
private set;
}
public static double ElapsedTime
{
get
{
long frequency, performanceCount;
QueryPerformanceFrequency(out frequency);
QueryPerformanceCounter(out performanceCount);
double thisTime = performanceCount / (double)frequency;
if (!startTime.HasValue)
{
startTime = thisTime;
lastTime = thisTime;
return 0.0;
}
LastDeltaTime = thisTime - lastTime;
lastTime = thisTime;
return thisTime - startTime.Value;
}
}
}
}

View File

@@ -0,0 +1,70 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common
{
public class ObjectFactory
{
private Dictionary<Type, Type> m_Patterns = new Dictionary<Type, Type>();
private Dictionary<Type, Dictionary<string, object>> m_StoredObjects = new Dictionary<Type, Dictionary<string, object>>();
public void RegisterPattern<O>()
{
var patternObject = typeof(O);
var patternInterface = patternObject.GetInterface("I" + patternObject.Name);
m_Patterns.Add(patternInterface, patternObject);
}
public void RegisterPattern<I, O>()
{
m_Patterns.Add(typeof(I), typeof(O));
}
public T CreateInstance<T>(params object[] args)
{
var creationType = m_Patterns.First(argPattern => argPattern.Key == typeof(T)).Value;
Type[] creationArguments = null;
if (args != null && args.Length > 0)
creationArguments = args.Select(arg => arg.GetType()).ToArray();
var constructor = creationType.GetConstructor(creationArguments == null ?
System.Type.EmptyTypes : creationArguments);
var construct = (T)constructor.Invoke(args);
return construct;
}
public T CreateNamedInstance<T>(string argName, params object[] args)
{
var construct = CreateInstance<T>(args);
var storage = m_StoredObjects.FirstOrDefault(type => type.Key == typeof(T)).Value;
if (storage == null)
{
m_StoredObjects.Add(typeof(T), new Dictionary<string, object>());
storage = m_StoredObjects.FirstOrDefault(type => type.Key == typeof(T)).Value;
}
storage.Add(argName, construct);
return construct;
}
public T FindNamedInstance<T>(string argName)
{
var storage = m_StoredObjects.FirstOrDefault(type => type.Key == typeof(T)).Value;
if (storage == null || !storage.ContainsKey(argName))
return default(T);
return (T)storage[argName];
}
public void RemoveNamedInstance<T>(string argName)
{
var storage = m_StoredObjects.FirstOrDefault(type => type.Key == typeof(T)).Value;
if (storage == null)
return;
storage.Remove(argName);
}
}
}

View File

@@ -0,0 +1,23 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Aiwaz.Common
{
public class Pair<T, U>
{
public Pair()
{
}
public Pair(T first, U second)
{
this.First = first;
this.Second = second;
}
public T First { get; set; }
public U Second { get; set; }
}
}

View File

@@ -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("AiwazCommon")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("Microsoft")]
[assembly: AssemblyProduct("AiwazCommon")]
[assembly: AssemblyCopyright("Copyright © Microsoft 2010")]
[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("e686f0df-9858-4478-a7fb-dea091646ff0")]
// 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")]

View File

@@ -0,0 +1,22 @@
using Aiwaz.Common.Animations;
namespace Aiwaz.Common
{
public static class Timeline
{
private static double realTime;
public static double RealTime { get { return realTime; } }
private static double demoTime;
public static double DemoTime { get { return demoTime; } }
public static double SpeedAndDirection { get; set; }
private static double lastRealDeltaTime;
public static void Advance(double argSeconds)
{
lastRealDeltaTime = argSeconds;
realTime += argSeconds;
demoTime += argSeconds * SpeedAndDirection;
AnimationManager.Animate();
}
}
}

View File

@@ -0,0 +1,42 @@
using System;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;
using Microsoft.Win32.SafeHandles;
namespace Aiwaz.Common
{
public class Win32Console : IDisposable
{
[DllImport("kernel32.dll", EntryPoint = "FreeConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int FreeConsole();
[DllImport("kernel32.dll", EntryPoint = "GetStdHandle", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern IntPtr GetStdHandle(int nStdHandle);
[DllImport("kernel32.dll", EntryPoint = "AllocConsole", SetLastError = true, CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
private static extern int AllocConsole();
private const int STD_OUTPUT_HANDLE = -11;
private const int MY_CODE_PAGE = 437;
public Win32Console()
{
AllocConsole();
IntPtr stdHandle = GetStdHandle(STD_OUTPUT_HANDLE);
SafeFileHandle safeFileHandle = new SafeFileHandle(stdHandle, true);
FileStream fileStream = new FileStream(safeFileHandle, FileAccess.Write);
Encoding encoding = System.Text.Encoding.GetEncoding(MY_CODE_PAGE);
StreamWriter standardOutput = new StreamWriter(fileStream, encoding);
standardOutput.AutoFlush = true;
Console.SetOut(standardOutput);
}
#region IDisposable Members
public void Dispose()
{
FreeConsole();
}
#endregion
}
}