Add project files.

This commit is contained in:
2022-09-30 17:32:21 +08:00
parent df69e65c88
commit e8e36b83bd
561 changed files with 40626 additions and 0 deletions

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: d213440bfd40d0d49a2950e04f3008ab
folderAsset: yes
timeCreated: 1594270605
licenseType: Pro
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
using System;
namespace Cryville.Common {
public class AsyncDelivery<T> {
public Action CancelSource { private get; set; }
public Action<bool, T> Destination { private get; set; }
public void Deliver(bool succeeded, T result) {
if (Destination != null) Destination(succeeded, result);
}
public void Cancel() {
if (CancelSource != null) CancelSource();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 857c1f0e990462a47bd0ed83448f923b
timeCreated: 1637755775
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,59 @@
using System;
using System.Globalization;
using System.Reflection;
namespace Cryville.Common {
public class BinderAttribute : Attribute {
public BinderAttribute(Type type) {
BinderType = type;
}
public Type BinderType;
public static Binder CreateBinderOfType(Type type) {
var l = type.GetCustomAttributes(typeof(BinderAttribute), true);
if (l.Length > 0) {
return (Binder)ReflectionHelper.InvokeEmptyConstructor(
((BinderAttribute)l[0]).BinderType
);
}
return new EmptyBinder();
}
}
public class EmptyBinder : Binder {
/*static readonly Type[] emptyTypeArray = {};
static readonly object[] emptyObjectArray = {};*/
public override FieldInfo BindToField(BindingFlags bindingAttr, FieldInfo[] match, object value, CultureInfo culture) {
throw new NotImplementedException();
}
public override MethodBase BindToMethod(BindingFlags bindingAttr, MethodBase[] match, ref object[] args, ParameterModifier[] modifiers, CultureInfo culture, string[] names, out object state) {
throw new NotImplementedException();
}
public override object ChangeType(object value, Type type, CultureInfo culture) {
if (value == null)
return null;
else if (type == value.GetType())
return value;
else if (type.IsEnum && value is string) {
return Enum.Parse(type, (string)value);
}
throw new InvalidCastException();
}
public override void ReorderArgumentArray(ref object[] args, object state) {
throw new NotImplementedException();
}
public override MethodBase SelectMethod(BindingFlags bindingAttr, MethodBase[] match, Type[] types, ParameterModifier[] modifiers) {
throw new NotImplementedException();
}
public override PropertyInfo SelectProperty(BindingFlags bindingAttr, PropertyInfo[] match, Type returnType, Type[] indexes, ParameterModifier[] modifiers) {
throw new NotImplementedException();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 146a308dba289ad4f91c07c69bb4688b
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 53f4e3167a1eee2478b0abc6302aee8f
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,65 @@
namespace Cryville.Common.Buffers {
/// <summary>
/// A resource pool that allows reusing instances of arrays of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The item type of the arrays in the pool.</typeparam>
public class ArrayPool<T> {
private class Bucket : ObjectPool<T[]> {
readonly int _size;
public Bucket(int size, int capacity) : base(capacity) {
_size = size;
}
protected override T[] Construct() {
return new T[_size];
}
}
Bucket[] _buckets;
/// <summary>
/// Creates an instance of the <see cref="ArrayPool{T}" /> class with the default maximum list size and bucket capacity.
/// </summary>
public ArrayPool() : this(0x40000000, 256) { }
/// <summary>
/// Creates an instance of the <see cref="ArrayPool{T}" /> class.
/// </summary>
/// <param name="maxSize">The maximum size of the arrays in the pool.</param>
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups arrays of similar sizes into buckets for faster access.</param>
public ArrayPool(int maxSize, int capacityPerBucket) {
if (maxSize < 16) maxSize = 16;
int num = GetID(maxSize) + 1;
_buckets = new Bucket[num];
for (int i = 0; i < num; i++) {
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
}
}
/// <summary>
/// Rents an array that is at least the specified size from the pool.
/// </summary>
/// <param name="size">The minimum size of the array.</param>
/// <returns>An array of type <see cref="T" /> that is at least the specified size.</returns>
public T[] Rent(int size) {
int len2 = size;
if (len2 < 16) len2 = 16;
var arr = _buckets[GetID(len2)].Rent();
return arr;
}
/// <summary>
/// Returns a rented array to the pool.
/// </summary>
/// <param name="arr">The array to return.</param>
public void Return(T[] arr) {
int len2 = arr.Length;
if (len2 < 16) len2 = 16;
_buckets[GetID(len2)].Return(arr);
}
static int GetID(int size) {
size -= 1;
size >>= 4;
int num = 0;
for (; size != 0; size >>= 1) num++;
return num;
}
static int GetSize(int id) {
return 0x10 << id;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: df66519fa93e1b94ea5bb1702cc91b3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,71 @@
using System.Collections.Generic;
namespace Cryville.Common.Buffers {
/// <summary>
/// A resource pool that allows reusing instances of lists of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The item type of the lists in the pool.</typeparam>
public class ListPool<T> {
private class Bucket : ObjectPool<List<T>> {
readonly int _size;
public Bucket(int size, int capacity) : base(capacity) {
_size = size;
}
protected override List<T> Construct() {
return new List<T>(_size);
}
}
Bucket[] _buckets;
/// <summary>
/// Creates an instance of the <see cref="ListPool{T}" /> class with the default maximum list size and bucket capacity.
/// </summary>
public ListPool() : this(0x40000000, 256) { }
/// <summary>
/// Creates an instance of the <see cref="ListPool{T}" /> class.
/// </summary>
/// <param name="maxSize">The maximum size of the lists in the pool.</param>
/// <param name="capacityPerBucket">The capacity of each bucket. The pool groups lists of similar sizes into buckets for faster access.</param>
public ListPool(int maxSize, int capacityPerBucket) {
if (maxSize < 16) maxSize = 16;
int num = GetID(maxSize) + 1;
_buckets = new Bucket[num];
for (int i = 0; i < num; i++) {
_buckets[i] = new Bucket(GetSize(i), capacityPerBucket);
}
}
/// <summary>
/// Rents a list of the specified size from the pool. The size of the list must not be changed when it is rented.
/// </summary>
/// <param name="size">The size of the list.</param>
/// <returns>A <see cref="List{T}" /> of the specified size.</returns>
public List<T> Rent(int size) {
int len2 = size;
if (len2 < 16) len2 = 16;
var list = _buckets[GetID(len2)].Rent();
if (list.Count < size)
for (int i = list.Count; i < size; i++) list.Add(default(T));
else if (list.Count > size)
list.RemoveRange(size, list.Count - size);
return list;
}
/// <summary>
/// Returns a rented list to the pool.
/// </summary>
/// <param name="list">The list to return.</param>
public void Return(List<T> list) {
int len2 = list.Capacity;
if (len2 < 16) len2 = 16;
_buckets[GetID(len2)].Return(list);
}
static int GetID(int size) {
size -= 1;
size >>= 4;
int num = 0;
for (; size != 0; size >>= 1) num++;
return num;
}
static int GetSize(int id) {
return 0x10 << id;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2b7b45ff20c33ac47b476371673b037c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,42 @@
namespace Cryville.Common.Buffers {
/// <summary>
/// A resource pool that allows reusing instances of type <typeparamref name="T" />.
/// </summary>
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
public abstract class ObjectPool<T> where T : class {
int _index;
readonly T[] _objs;
/// <summary>
/// Creates an instance of the <see cref="ObjectPool{T}" /> class.
/// </summary>
/// <param name="capacity">The capacity of the pool.</param>
public ObjectPool(int capacity) {
_objs = new T[capacity];
}
/// <summary>
/// Rents a object from the pool.
/// </summary>
/// <returns>The rented object.</returns>
public T Rent() {
T obj = null;
if (_index < _objs.Length) {
obj = _objs[_index];
_objs[_index++] = null;
}
if (obj == null) obj = Construct();
return obj;
}
/// <summary>
/// Returns a rented object to the pool.
/// </summary>
/// <param name="obj">The object to return.</param>
public void Return(T obj) {
if (_index > 0) _objs[--_index] = obj;
}
/// <summary>
/// Constructs a new instance of type <typeparamref name="T" />.
/// </summary>
/// <returns>The new instance.</returns>
protected abstract T Construct();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2745c44c3cc32be4ab3a43888c14b9a1
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,16 @@
namespace Cryville.Common.Buffers {
/// <summary>
/// A resource pool that allows reusing instances of type <typeparamref name="T" />, which has a parameterless constructor.
/// </summary>
/// <typeparam name="T">The type of the objects in the pool.</typeparam>
public class SimpleObjectPool<T> : ObjectPool<T> where T : class, new() {
/// <summary>
/// Creates an instance of the <see cref="SimpleObjectPool{T}" /> class.
/// </summary>
/// <param name="capacity">The capacity of the pool.</param>
public SimpleObjectPool(int capacity) : base(capacity) { }
protected override T Construct() {
return new T();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: b8cd439340f088d4eb83711a5bc6384d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,10 @@
using System;
using System.Runtime.InteropServices;
namespace Cryville.Common.Buffers {
public class WStringPool {
public WStringPool() {
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 704270b37917aa1458db9d14bab07073
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 3c27afc8f4f76d04dac0f0914798ccc0
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
using System;
namespace Cryville.Common.ComponentModel {
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class LogarithmicScaleAttribute : Attribute {
public LogarithmicScaleAttribute() { }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: c1e8a5b839ed6a64dbb9fc6b0bcf7cc2
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,12 @@
using System;
namespace Cryville.Common.ComponentModel {
[AttributeUsage(AttributeTargets.Property, Inherited = false)]
public class PrecisionAttribute : Attribute {
public PrecisionAttribute(double precision) {
Precision = precision;
}
public double Precision { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: ea83b8e303a1b8b43a2f2ff74a7a9a7a
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,14 @@
using System;
namespace Cryville.Common.ComponentModel {
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class RangeAttribute : Attribute {
public RangeAttribute(float min, float max) {
Min = min;
Max = max;
}
public float Min { get; set; }
public float Max { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 569e766a56b73244dbade8de4c525faa
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,11 @@
using System;
namespace Cryville.Common.ComponentModel {
[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
public class StepAttribute : Attribute {
public StepAttribute(float step) {
Step = step;
}
public float Step { get; set; }
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 94340a38155591146ada89db63bc4aeb
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,17 @@
using System;
namespace Cryville.Common {
public class FileStringAttribute : Attribute {
private readonly string filter;
public string[] Filter {
get {
return filter.Split('|');
}
}
public FileStringAttribute(string ext) {
filter = ext;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c3041eaac507d7548ae3460b33230271
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 7ad09f227a3c83141b6d9a0f55b4cb38
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
using System.Globalization;
namespace Cryville.Common.Font {
public static class FontUtil {
/*public static string MatchFontNameWithLang(string lang) {
}*/
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f3cd70779125d96409f5a299a8034f5f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,27 @@
using System;
using System.IO;
using System.Text;
namespace Cryville.Common {
public static class IOExtensions {
public static DirectoryInfo GetSubdirectory(this DirectoryInfo dir, string name) {
var l1 = dir.GetDirectories(name);
if (l1.Length == 0) return dir.CreateSubdirectory(name);
else return l1[0];
}
public static string ReadUInt16String(this BinaryReader reader, Encoding encoding = null) {
if (encoding == null) encoding = Encoding.UTF8;
var len = reader.ReadUInt16();
byte[] buffer = reader.ReadBytes(len);
return encoding.GetString(buffer);
}
public static void WriteUInt16String(this BinaryWriter writer, string value, Encoding encoding = null) {
if (encoding == null) encoding = Encoding.UTF8;
byte[] buffer = encoding.GetBytes(value);
writer.Write((ushort)buffer.Length);
writer.Write(buffer);
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 49f85de87c5e683429e4790c0dc3fd2c
timeCreated: 1620706538
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,79 @@
using Ionic.Zip;
using System;
using System.Collections.Generic;
using System.IO;
namespace Cryville.Common {
public abstract class Logger {
static readonly Dictionary<string, Logger> Instances = new Dictionary<string, Logger>();
static readonly Dictionary<string, StreamWriter> Files = new Dictionary<string, StreamWriter>();
static string logPath = null;
public static void SetLogPath(string path) {
logPath = path;
var dir = new DirectoryInfo(path);
if (!dir.Exists) Directory.CreateDirectory(dir.FullName);
}
public static void Log(string key, int level, string module, string format, params object[] args) {
if (!Instances.ContainsKey(key)) return;
Instances[key].Log(level, module, string.Format(format, args));
if (Files.ContainsKey(key)) Files[key].WriteLine("[{0:O}] [{1}] <{2}> {3}", DateTime.UtcNow, level, module, string.Format(format, args));
}
public static void Create(string key, Logger logger) {
Instances[key] = logger;
if (logPath != null) {
Files[key] = new StreamWriter(logPath + "/" + ((int)DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1)).TotalSeconds).ToString() + "-" + key + ".log");
Files[key].AutoFlush = true;
}
}
public static void Close() {
Instances.Clear();
foreach (var f in Files) f.Value.Dispose();
Files.Clear();
}
public virtual void Log(int level, string module, string msg) { }
}
public class InstantLogger : Logger {
readonly Action<int, string, string> callback;
public InstantLogger(Action<int, string, string> callback) {
if (callback == null)
throw new ArgumentNullException("callback");
this.callback = callback;
}
public override void Log(int level, string module, string msg) {
base.Log(level, module, msg);
callback(level, module, msg);
}
}
public class BufferedLogger : Logger {
readonly List<LogEntry> buffer = new List<LogEntry>();
public BufferedLogger() { }
public override void Log(int level, string module, string msg) {
base.Log(level, module, msg);
lock (buffer) {
buffer.Add(new LogEntry(level, module, msg));
}
}
public void Enumerate(Action<int, string, string> callback) {
lock (buffer) {
foreach (var i in buffer) {
callback(i.level, i.module, i.msg);
}
}
buffer.Clear();
}
}
public struct LogEntry {
public int level;
public string module;
public string msg;
public LogEntry(int level, string module, string msg) {
this.level = level;
this.module = module;
this.msg = msg;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1c1729cfde78f1c479c9f7eb166e0107
timeCreated: 1611126212
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: c7912778bf022c34592b3ebf16782635
folderAsset: yes
timeCreated: 1616377089
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,37 @@
namespace Cryville.Common.Math {
public class ColumnVector<T> {
readonly T[] content;
public int Size {
get;
private set;
}
public ColumnVector(int size) {
content = new T[size];
Size = size;
}
public ColumnVector(T[] c) {
Size = c.Length;
content = c;
}
public T this[int i] {
get {
return content[i];
}
set {
content[i] = value;
}
}
public T Dot(ColumnVector<float> lhs, IVectorOperator<T> o) {
T res = default(T);
for (var i = 0; i < Size; i++)
res = o.Add(res, o.ScalarMultiply(lhs[i], content[i]));
return res;
}
public static ColumnVector<float> WithPolynomialCoefficients(int size, float num) {
var m = new ColumnVector<float>(size);
for (var i = 0; i < size; i++)
m[i] = (float)System.Math.Pow(num, i);
return m;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: b99c66d83f1330841a0c5a23e87bf873
timeCreated: 1616406828
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,6 @@
namespace Cryville.Common.Math {
public interface IVectorOperator<T> {
T Add(T lhs, T rhs);
T ScalarMultiply(float lhs, T rhs);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6bd0295c670699c49b6f0944832387a9
timeCreated: 1616379780
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,63 @@
namespace Cryville.Common.Math {
public class SquareMatrix {
readonly float[,] content;
public int Size {
get;
private set;
}
public SquareMatrix(int size) {
content = new float[size, size];
Size = size;
}
public float this[int r, int c] {
get { return content[r, c]; }
set { content[r, c] = value; }
}
public ColumnVector<T> Eliminate<T>(ColumnVector<T> v, IVectorOperator<T> o) {
int s = Size;
float[,] d = (float[,])content.Clone();
int[] refl = new int[s];
for (int i = 0; i < s; i++)
refl[i] = i;
for (int r = 0; r < s; r++) {
for (int r0 = r; r0 < s; r0++)
if (d[refl[r0], r] != 0) {
refl[r] = r0;
refl[r0] = r;
break;
}
int or = refl[r];
float sf0 = d[or, r];
for (int c0 = r; c0 < s; c0++)
d[or, c0] /= sf0;
v[or] = o.ScalarMultiply(1 / sf0, v[or]);
for (int r1 = r + 1; r1 < s; r1++) {
int or1 = refl[r1];
float sf1 = d[or1, r];
for (int c1 = r; c1 < s; c1++)
d[or1, c1] -= d[or, c1] * sf1;
v[or1] = o.Add(v[or1], o.ScalarMultiply(-sf1, v[or]));
}
}
T[] res = new T[s];
for (int r2 = s - 1; r2 >= 0; r2--) {
var v2 = v[refl[r2]];
for (int c2 = r2 + 1; c2 < s; c2++)
v2 = o.Add(v2, o.ScalarMultiply(-d[refl[r2], c2], res[refl[c2]]));
res[refl[r2]] = v2;
}
return new ColumnVector<T>(res);
}
public static SquareMatrix WithPolynomialCoefficients(int size) {
var m = new SquareMatrix(size);
for (var r = 0; r < size; r++) {
int d = 1;
for (var c = 0; c < size; c++) {
m[r, c] = d;
d *= r;
}
}
return m;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: bceea65ebaa5052409eb85086645232e
timeCreated: 1616377102
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: f8303a3eeefeacf4ca0c02b5d32e0cff
folderAsset: yes
timeCreated: 1621071543
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,142 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Text;
using Microsoft.Win32;
namespace Cryville.Common.Network {
public class HttpClient {
private readonly string _directHost;
protected string DirectHost { get { return _directHost; } }
private readonly int _directPort;
protected int DirectPort { get { return _directPort; } }
readonly Uri _baseUri;
readonly int origPort;
protected string Version = "HTTP/1.1";
protected TcpClient TcpClient;
protected virtual Stream Stream {
get {
return TcpClient.GetStream();
}
}
private readonly bool _proxied = false;
public Dictionary<string, string> Headers { get; set; }
public HttpClient(Uri baseUri, int port = 80) {
_directHost = baseUri.Host;
_directPort = port;
_baseUri = baseUri;
origPort = _baseUri.Port;
Headers = new Dictionary<string, string>();
_proxied = GetProxy(ref _directHost, ref _directPort);
Logger.Log("main", 0, "Network", "Connecting to {0}:{1}", DirectHost, DirectPort);
TcpClient = new TcpClient(DirectHost, DirectPort);
}
public virtual void Connect() {
if (_proxied) {
Request("CONNECT", _baseUri.Host + ":" + origPort.ToString());
}
}
public virtual void Close() {
TcpClient.Close();
}
public HttpResponse Request(string method, Uri uri, string body = null, Encoding encoding = null) {
string struri = GetUri(uri).PathAndQuery;
// if (_proxied) struri = GetUri(uri).AbsoluteUri;
return Request(method, struri, body, encoding);
}
public HttpResponse Request(string method, string uri, string body = null, Encoding encoding = null) {
var headers = new Dictionary<string, string>();
// if (Stream.CanTimeout) Stream.ReadTimeout = Stream.WriteTimeout = 5000;
foreach (var h in Headers)
headers.Add(h.Key, h.Value);
// headers["Accept"] = "text/plain, */*";
// headers["Connection"] = "close";
headers["Host"] = _baseUri.Host;
byte[] payload = new byte[0];
if (body != null) {
if (encoding == null)
encoding = Encoding.UTF8;
payload = encoding.GetBytes(body);
headers.Add("Content-Encoding", encoding.EncodingName);
headers.Add("Content-Length", payload.Length.ToString());
}
string request_line = string.Format(
"{0} {1} {2}\r\n", method, uri, Version
);
string header_fields = string.Concat((
from h in headers select h.Key + ":" + h.Value + "\r\n"
).ToArray());
byte[] buffer0 = Encoding.ASCII.GetBytes(string.Format(
"{0}{1}\r\n", request_line, header_fields
));
byte[] buffer1 = new byte[buffer0.Length + payload.Length];
Array.Copy(buffer0, buffer1, buffer0.Length);
Array.Copy(payload, 0, buffer1, buffer0.Length, payload.Length);
Logger.Log("main", 0, "Network", Encoding.UTF8.GetString(buffer1));
Stream.Write(buffer1, 0, buffer1.Length);
Stream.Flush();
var response = new HttpResponse(Stream);
Logger.Log("main", 0, "Network", "{0}", response);
return response;
}
protected virtual bool GetProxy(ref string host, ref int port) {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
var proxyEnable = (int)reg.GetValue("ProxyEnable");
if (proxyEnable == 0) return false;
var proxyStr = (string)reg.GetValue("ProxyServer");
if (!string.IsNullOrEmpty(proxyStr)) {
string[] proxies = proxyStr.Split(';');
foreach (var p in proxies) {
if (p.StartsWith("http=")) {
string[] s = p.Split('=', ':');
host = s[1];
port = int.Parse(s[2]);
return true;
}
}
}
}
return false;
}
protected Uri GetUri(string path) {
Uri address;
if (_baseUri != null) {
if (!Uri.TryCreate(_baseUri, path, out address)) {
return new Uri(Path.GetFullPath(path));
}
}
else {
if (!Uri.TryCreate(path, UriKind.Absolute, out address)) {
return new Uri(Path.GetFullPath(path));
}
}
return GetUri(address);
}
protected Uri GetUri(Uri address) {
if (address == null) {
throw new ArgumentNullException("address");
}
Uri uri = address;
if (!address.IsAbsoluteUri && _baseUri != null && !Uri.TryCreate(_baseUri, address, out uri)) {
return address;
}
return uri;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 5ea931bf5488011468f3d1243a038874
timeCreated: 1622589817
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,61 @@
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace Cryville.Common.Network {
public class HttpResponse {
static readonly char[] spchar = new char[]{ ' ' };
public string HttpVersion { get; private set; }
public string StatusCode { get; private set; }
public string ReasonPhase { get; private set; }
public Dictionary<string, string> Headers { get; private set; }
public HttpResponseStream MessageBody { get; private set; }
internal HttpResponse(Stream stream) {
var reader = new BinaryReader(stream, Encoding.ASCII);
var statu_line = ReadLine(reader).Split(spchar, 3);
HttpVersion = statu_line[0];
StatusCode = statu_line[1];
ReasonPhase = statu_line[2];
Logger.Log("main", 0, "Network", "Receive Response: {0} {1} {2}", HttpVersion, StatusCode, ReasonPhase);
Headers = new Dictionary<string, string>();
while (ParseHeader(reader, Headers)) ;
if (Headers.ContainsKey("content-length")) {
int length = int.Parse(Headers["content-length"]);
MessageBody = new HttpResponseBlockStream(reader, length);
}
else if (Headers.ContainsKey("transfer-encoding") && Headers["transfer-encoding"] == "chunked") {
MessageBody = new HttpResponseChunkedStream(reader);
}
}
public override string ToString() {
return string.Format("<{0} {1} {2}>", HttpVersion, StatusCode, ReasonPhase);
}
internal static bool ParseHeader(BinaryReader reader, Dictionary<string,string> headers) {
// TODO Multiline header
var header = ReadLine(reader);
if (header == "") return false;
var s = header.Split(':');
string field_name = s[0].Trim().ToLower();
string field_value = s[1].Trim();
if (headers.ContainsKey(field_name)) headers[field_name] += "," + field_value;
else headers.Add(field_name, field_value);
Logger.Log("main", 0, "Network", "Receive Header {0}: {1}", field_name, field_value);
return true;
}
internal static string ReadLine(BinaryReader reader) {
StringBuilder result = new StringBuilder();
char c;
while (true) {
c = reader.ReadChar();
if (c == '\r') break;
result.Append(c);
}
// TODO Unseekable
reader.ReadByte();
return result.ToString();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 07e8215a93e3eb1418685009f0c58dcd
timeCreated: 1622596274
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,135 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
namespace Cryville.Common.Network {
public abstract class HttpResponseStream : Stream {
public override bool CanRead { get { return true; } }
public override bool CanSeek { get { return false; } }
public override bool CanWrite { get { return false; } }
public override long Length { get { throw new NotSupportedException(); } }
public override long Position {
get { throw new NotSupportedException(); }
set { throw new NotSupportedException(); }
}
public override void Flush() {
// Do nothing
}
public abstract byte[] ReadToEnd();
public override long Seek(long offset, SeekOrigin origin) {
throw new NotSupportedException();
}
public override void SetLength(long value) {
throw new NotSupportedException();
}
public override void Write(byte[] buffer, int offset, int count) {
throw new NotSupportedException();
}
}
internal sealed class HttpResponseBlockStream : HttpResponseStream {
readonly BinaryReader _reader;
readonly int _length;
int _pos = 0;
internal HttpResponseBlockStream(BinaryReader reader, int length) {
_reader = reader;
_length = length;
}
public override int Read(byte[] buffer, int offset, int count) {
int recv = 0;
int recv_len = System.Math.Min(count, _length - _pos);
if (recv_len == 0) return 0;
while (recv < recv_len) {
recv += _reader.Read(buffer, offset + recv, count - recv);
Logger.Log("main", 0, "Network", "Message body received: {0}/{1}/{2}", recv, recv_len, _length);
}
_pos += recv_len;
return recv_len;
}
public override byte[] ReadToEnd() {
byte[] buffer = new byte[_length - _pos];
Read(buffer, 0, buffer.Length);
return buffer;
}
}
internal sealed class HttpResponseChunkedStream : HttpResponseStream {
readonly BinaryReader _reader;
byte[] _chunk = null;
int _pos = 0;
internal HttpResponseChunkedStream(BinaryReader reader) {
_reader = reader;
ReadChunk();
}
public void ReadChunk() {
if (_chunk != null && _chunk.Length == 0) return;
string[] chunkHeader = HttpResponse.ReadLine(_reader).Split(';');
// int chunkSize = Array.IndexOf(LEN, chunkHeader[0].ToLower()[0]);
int chunkSize = int.Parse(chunkHeader[0], NumberStyles.HexNumber);
if (chunkSize == -1)
throw new IOException("Corrupted chunk received");
if (chunkSize == 0) {
_chunk = new byte[0];
// TODO TE Header, now just discard
var headers = new Dictionary<string, string>();
while (HttpResponse.ParseHeader(_reader, headers)) ;
return;
}
_chunk = new byte[chunkSize];
int recv = 0;
while (recv < chunkSize) {
recv += _reader.Read(_chunk, recv, chunkSize - recv);
Logger.Log("main", 0, "Network", "Message chunk received: {0}/{1}", recv, chunkSize);
}
_pos = 0;
if (HttpResponse.ReadLine(_reader) != "")
throw new IOException("Corrupted chunk received");
}
public override int Read(byte[] buffer, int offset, int count) {
if (_chunk.Length == 0) return 0;
int recv = 0;
while (true) {
if (count - recv <= _chunk.Length - _pos) break;
Array.Copy(_chunk, _pos, buffer, recv, _chunk.Length - _pos);
recv += _chunk.Length - _pos;
ReadChunk();
if (_chunk.Length == 0) return recv;
}
Array.Copy(_chunk, _pos, buffer, recv, count - recv);
return count;
}
public override byte[] ReadToEnd() {
if (_chunk.Length == 0) return new byte[0];
List<byte[]> segs = new List<byte[]>();
while (true) {
if (_pos != 0) {
var buffer = new byte[_chunk.Length - _pos];
Array.Copy(_chunk, _pos, buffer, 0, buffer.Length);
segs.Add(buffer);
}
else segs.Add(_chunk);
ReadChunk();
if (_chunk.Length == 0) {
var result = new byte[segs.Sum(i => i.Length)];
int p = 0;
foreach (var i in segs) {
Array.Copy(i, 0, result, p, i.Length);
p += i.Length;
}
return result;
}
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: f191de447a708da4f9d230e6545ce0a6
timeCreated: 1635470462
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,51 @@
using Microsoft.Win32;
using System;
using System.IO;
using System.Net.Sockets;
namespace Cryville.Common.Network {
public class HttpsClient : HttpClient {
readonly TlsTcpClient _tlsTcpClient;
protected override Stream Stream {
get {
return _tlsTcpClient.Stream;
}
}
public HttpsClient(Uri baseUri) : base(baseUri, 443) {
_tlsTcpClient = new TlsTcpClient(DirectHost, DirectPort);
}
public override void Connect() {
_tlsTcpClient.Connect();
base.Connect();
}
public override void Close() {
base.Close();
_tlsTcpClient.Close();
}
protected override bool GetProxy(ref string host, ref int port) {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
var reg = Registry.CurrentUser.OpenSubKey(@"Software\Microsoft\Windows\CurrentVersion\Internet Settings");
var proxyEnable = (int)reg.GetValue("ProxyEnable");
if (proxyEnable == 0) return false;
var proxyStr = (string)reg.GetValue("ProxyServer");
if (!string.IsNullOrEmpty(proxyStr)) {
string[] proxies = proxyStr.Split(';');
foreach (var p in proxies) {
if (p.StartsWith("https=")) {
string[] s = p.Split('=', ':');
host = s[1];
port = int.Parse(s[2]);
return true;
}
}
}
}
return false;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9b35290e0e147a342acc29a20c8fceaf
timeCreated: 1622503538
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,94 @@
using Org.BouncyCastle.Security;
using Org.BouncyCastle.Tls;
using Org.BouncyCastle.Tls.Crypto;
using Org.BouncyCastle.Tls.Crypto.Impl.BC;
using System.Collections;
using System.IO;
using System.Linq;
using System.Net.Sockets;
namespace Cryville.Common.Network {
public class TlsTcpClient {
readonly TcpClient _tcpClient;
readonly TlsClientProtocol _protocol;
readonly TlsClient _tlsClient;
public Stream Stream { get; private set; }
public TlsTcpClient(string hostname, int port) {
_tcpClient = new TcpClient(hostname, port);
_protocol = new TlsClientProtocol(_tcpClient.GetStream());
_tlsClient = new InternalTlsClient(new BcTlsCrypto(new SecureRandom()));
}
public void Connect() {
_protocol.Connect(_tlsClient);
Stream = _protocol.Stream;
}
public void Close() {
_protocol.Close();
}
private class InternalTlsClient : DefaultTlsClient {
public InternalTlsClient(TlsCrypto crypto) : base(crypto) { }
protected override ProtocolVersion[] GetSupportedVersions() {
return ProtocolVersion.TLSv13.DownTo(ProtocolVersion.TLSv12);
}
protected override IList GetProtocolNames() {
IList list = new ArrayList {
ProtocolName.Http_1_1,
ProtocolName.Http_2_Tls
};
return list;
}
private static readonly int[] supportedCipherSuites = {
CipherSuite.TLS_AES_128_GCM_SHA256,
CipherSuite.TLS_AES_256_GCM_SHA384,
CipherSuite.TLS_CHACHA20_POLY1305_SHA256,
CipherSuite.TLS_AES_128_CCM_SHA256,
CipherSuite.TLS_AES_128_CCM_8_SHA256,
};
protected override int[] GetSupportedCipherSuites() {
return base.GetSupportedCipherSuites().Union(supportedCipherSuites).ToArray();
}
protected override IList GetSupportedSignatureAlgorithms() {
var result = base.GetSupportedSignatureAlgorithms();
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP256r1tls13_sha256);
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP384r1tls13_sha384);
result.Add(SignatureAndHashAlgorithm.ecdsa_brainpoolP512r1tls13_sha512);
return result;
}
public override TlsAuthentication GetAuthentication() {
return new NullTlsAuthentication();
}
public override void NotifyAlertReceived(short alertLevel, short alertDescription) {
Logger.Log("main", 0, "Network/TLS", "TLS Alert {0} {1}", alertLevel, alertDescription);
}
public override void NotifyServerVersion(ProtocolVersion serverVersion) {
base.NotifyServerVersion(serverVersion);
Logger.Log("main", 0, "Network/TLS", "NotifyServerVersion {0}", serverVersion);
}
public override void NotifySelectedCipherSuite(int selectedCipherSuite) {
base.NotifySelectedCipherSuite(selectedCipherSuite);
Logger.Log("main", 0, "Network/TLS", "NotifySelectedCipherSuite {0}", selectedCipherSuite);
}
}
private class NullTlsAuthentication : TlsAuthentication {
public TlsCredentials GetClientCredentials(CertificateRequest certificateRequest) {
return null;
}
public void NotifyServerCertificate(TlsServerCertificate serverCertificate) {
// Do nothing
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: c9c242bb90fc1cc479a8df1407f21940
timeCreated: 1622021660
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: faaabaf75aa95c94c9671f2e89a01687
folderAsset: yes
timeCreated: 1605519813
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,155 @@
using System;
namespace Cryville.Common.Pdt {
/// <summary>
/// Base of evaluator for PDT expressions.
/// </summary>
public abstract class PdtEvaluatorBase {
private struct StackFrame {
public int Offset;
public int Length;
public int Type;
}
int _framecount = 0;
int _goffset = 0;
readonly StackFrame[] _stack = new StackFrame[256];
readonly byte[] _mem = new byte[0x100000];
bool _revokepttconst;
/// <summary>
/// Evaluates an expression and passes the result to a target operator.
/// </summary>
/// <param name="target">The target operator.</param>
/// <param name="exp">The expression to evaluate.</param>
public void Evaluate(PdtOperator target, PdtExpression exp) {
_framecount = 0;
_goffset = 0;
_revokepttconst = false;
for (var ins = exp.Instructions.First; ins != null; ins = ins.Next)
ins.Value.Execute(this);
Operate(target, _framecount, true);
if (exp.IsPotentialConstant) {
exp.IsConstant = exp.IsPotentialConstant = !_revokepttconst;
}
}
/// <summary>
/// Optimizes an expression by merging its instructions.
/// </summary>
/// <param name="exp">The expression to optimize.</param>
public void Optimize(PdtExpression exp) {
_framecount = 0;
_goffset = 0;
var il = exp.Instructions;
for (var ins = il.First; ins != null; ins = ins.Next) {
var i = ins.Value;
if (i is PdtInstruction.Operate) {
int fc0 = _framecount;
int fc1 = ((PdtInstruction.Operate)i).ParamCount;
try { i.Execute(this); } catch (Exception) { }
if (fc0 - _framecount == fc1) {
unsafe {
fixed (StackFrame* frame = &_stack[_framecount++]) {
frame->Type = PdtInternalType.Error;
frame->Offset = -1;
frame->Length = 0;
}
}
}
else {
var frame = _stack[_framecount - 1];
il.AddAfter(ins, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length));
ins = ins.Next;
for (var j = 0; j <= fc1; j++) il.Remove(ins.Previous);
}
}
else if (i is PdtInstruction.PushVariable) {
i.Execute(this);
var frame = _stack[_framecount - 1];
if (frame.Type != PdtInternalType.Undefined) {
il.AddAfter(ins, new PdtInstruction.PushConstant(frame.Type, _mem, frame.Offset, frame.Length));
ins = ins.Next;
il.Remove(ins.Previous);
}
}
else i.Execute(this);
}
exp.IsConstant = true;
exp.IsPotentialConstant = true;
for (var ins = il.First; ins != null; ins = ins.Next) {
if (!(ins.Value is PdtInstruction.PushConstant)) {
exp.IsConstant = false;
}
else if (!(ins.Value is PdtInstruction.PushVariable)) {
exp.IsPotentialConstant = false;
return;
}
}
}
/// <summary>
/// Revokes the potential constant mark of the current expression.
/// </summary>
protected void RevokePotentialConstant() {
_revokepttconst = true;
}
internal unsafe void PushConstant(int type, byte[] value) {
fixed (StackFrame* frame = &_stack[_framecount++]) {
frame->Type = type;
frame->Offset = _goffset;
frame->Length = value.Length;
Array.Copy(value, 0, _mem, _goffset, value.Length);
_goffset += value.Length;
}
}
internal unsafe void PushVariable(ref string name) {
fixed (StackFrame* frame = &_stack[_framecount++]) {
byte[] value;
GetVariable(name, out frame->Type, out value);
frame->Offset = _goffset;
frame->Length = value.Length;
Array.Copy(value, 0, _mem, _goffset, value.Length);
_goffset += value.Length;
}
}
/// <summary>
/// Gets a variable of the specified name.
/// </summary>
/// <param name="name">The name of the variable.</param>
/// <param name="type">The type of the variable.</param>
/// <param name="value">The value of the variable.</param>
protected abstract void GetVariable(string name, out int type, out byte[] value);
internal void Operate(ref string name, int pc) {
Operate(GetOperator(name, pc), pc);
}
/// <summary>
/// Gets an operator of the specified name and the suggested parameter count.
/// </summary>
/// <param name="name">The name of the operator.</param>
/// <param name="pc">Suggested parameter count.</param>
/// <returns>An operator of the specific name.</returns>
/// <remarks>The parameter count of the returned operator does not necessarily equal to <paramref name="pc" />.</remarks>
protected abstract PdtOperator GetOperator(string name, int pc);
bool _failure;
unsafe void Operate(PdtOperator op, int pc, bool noset = false) {
fixed (byte* pmem = _mem) {
op.Begin(this);
for (int i = 0; i < pc; i++) {
var frame = _stack[--_framecount];
op.LoadOperand(new PdtVariableMemory(frame.Type, pmem + frame.Offset, frame.Length));
_goffset -= frame.Length;
}
op.Call(pmem + _goffset, noset);
}
}
internal unsafe PdtVariableMemory StackAlloc(int type, byte* ptr, int len) {
fixed (StackFrame* frame = &_stack[_framecount++]) {
frame->Type = type;
frame->Offset = _goffset;
frame->Length = len;
_goffset += len;
return new PdtVariableMemory(type, ptr, len);
}
}
internal void DiscardStack() {
_goffset -= _stack[--_framecount].Length;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: e91dc9e4fd28a584c8f2fce4f9e9b648
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,247 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace Cryville.Common.Pdt {
/// <summary>
/// PDT expression.
/// </summary>
public class PdtExpression {
internal LinkedList<PdtInstruction> Instructions;
/// <summary>
/// Whether the value of this expression is constant.
/// </summary>
/// <remarks>The value of this property is <c>false</c> until it is optimized.</remarks>
public bool IsConstant { get; internal set; }
internal bool IsPotentialConstant;
internal PdtExpression(LinkedList<PdtInstruction> ins) {
Instructions = ins;
}
public override string ToString() {
string r = "";
bool flag = false;
foreach (PdtInstruction ins in Instructions) {
if (flag) r += "; ";
r += ins.ToString();
flag = true;
}
return r;
}
}
internal abstract class PdtInstruction {
internal abstract void Execute(PdtEvaluatorBase etor);
public class PushConstant : PdtInstruction {
public int Type { get; private set; }
public byte[] Value { get; private set; }
public PushConstant(int type, byte[] value) {
Type = type;
Value = value;
}
public PushConstant(int type, byte[] buffer, int offset, int len) {
Type = type;
Value = new byte[len];
Array.Copy(buffer, offset, Value, 0, len);
}
internal override void Execute(PdtEvaluatorBase etor) {
etor.PushConstant(Type, Value);
}
public override string ToString() {
return string.Format("pushc ({0:x8}){1}", Type, BitConverter.ToString(Value));
}
}
public class PushVariable : PdtInstruction {
private string m_name;
public string Name { get { return m_name; } }
public PushVariable(string name) {
m_name = name;
}
internal override void Execute(PdtEvaluatorBase etor) {
etor.PushVariable(ref m_name);
}
public override string ToString() {
return string.Format("pushv {0}", Name);
}
}
public class Operate : PdtInstruction {
private string m_name;
public string Name { get { return m_name; } }
public int ParamCount { get; private set; }
public Operate(string name, int paramCount) {
m_name = name;
ParamCount = paramCount;
}
internal override void Execute(PdtEvaluatorBase etor) {
etor.Operate(ref m_name, ParamCount);
}
public override string ToString() {
return string.Format("op {0}({1})", Name, ParamCount);
}
}
}
public partial class PdtInterpreter<T> {
readonly static Dictionary<char, int> pri = new Dictionary<char, int> {
{ '*', 5 }, { '/', 5 }, { '%', 5 },
{ '+', 4 }, { '-', 4 },
{ '=', 3 }, { '<', 3 }, { '>', 3 },
{ '&', 2 },
{ '|', 1 }, { '!', 1 },
{ ',', 0 },
};
readonly static PdtExpression _emptyexp;
static PdtInterpreter() {
var ins = new LinkedList<PdtInstruction>();
ins.AddLast(new PdtInstruction.PushConstant(
PdtInternalType.Number, BitConverter.GetBytes(1f)
));
_emptyexp = new PdtExpression(ins);
}
PdtExpToken GetToken() {
ws();
var result = new PdtExpToken {
Type = ct & 0x0fe0
};
switch (result.Type) {
case 0x0020: result.Value = GetIdentifier(); break;
case 0x0040: result.Value = GetNumber(); break;
case 0x0100: result.Value = GetString(); break;
default: result.Value = cc.ToString(); Position++; break;
}
return result;
}
private struct PdtExpToken {
public int Type { get; set; }
public string Value { get; set; }
public override string ToString() {
return string.Format("0x{0:x4}: {1}", Type, Value);
}
public readonly static PdtExpToken EmptyOperator = new PdtExpToken {
Type = 0x0080,
Value = "",
};
}
/// <summary>
/// Interprets an expression from the current position and loads it onto an instruction set.
/// </summary>
/// <param name="ins">The instruction set.</param>
/// <param name="enc">
/// <para>The enclosing scope type of the (sub)expression. May be one of the following:</para>
/// <list type="bullet">
/// <item><term><c>-2</c></term><description>The expression is in a root scope which ends at a semicolon.</description></item>
/// <item><term><c>-1</c></term><description>The expression is in a bracketed scope which ends at a closing parenthesis.</description></item>
/// <item><term>Any non-negative values</term><description>The expression is in an auxiliary scope which ends before a closing parenthesis or an operator whose priority is lower (less) than the value of this parameter.</description></item>
/// </list>
/// </param>
/// <param name="pc">The parameter count in this (sub)expression.</param>
/// <param name="token">The token already parsed but required for the interpretation of the subexpression.</param>
/// <returns>The expression token following this (sub)expression.</returns>
PdtExpToken InterpretExp(LinkedList<PdtInstruction> ins, int enc, out int pc, PdtExpToken? token = null) {
PdtExpToken t1, t2;
int insc0 = ins.Count;
pc = 1;
if (token == null) t1 = PdtExpToken.EmptyOperator;
else t1 = token.Value;
while (true) {
t2 = InterpretExpBlock(ins);
panic:
switch (t2.Type) {
case 0x0080:
if (t1.Value != "") {
int p1 = pri[t1.Value[0]];
int p2 = pri[t2.Value[0]];
if (p2 <= enc) goto exit;
if (p2 > p1) {
int _;
t2 = InterpretExp(ins, p1, out _, t2);
goto panic;
}
if (t1.Value != ",") ins.AddLast(new PdtInstruction.Operate(t1.Value, 2));
else pc++;
}
t1 = t2;
break;
case 0x0400:
if (enc == -2) throw new FormatException("Expression not enclosed correctly: Too many closing brackets");
if (ins.Count == insc0) pc = 0;
goto exit;
case 0x0800:
goto exit;
}
}
exit:
if (t1.Value != "," && t1.Value != "") ins.AddLast(new PdtInstruction.Operate(t1.Value, 2));
else if (t1.Value == ",") pc++;
return t2;
}
/// <summary>
/// Interprets an expression block which consists of several adjacent constants and subexpressions, optionally with a unary operator.
/// </summary>
/// <param name="ins">The instruction set.</param>
/// <returns>The expression token following this expression block.</returns>
PdtExpToken InterpretExpBlock(LinkedList<PdtInstruction> ins) {
var t = GetToken();
if (t.Type == 0x0080) {
var r = InterpretExpBlock(ins);
ins.AddLast(new PdtInstruction.Operate(t.Value, 1));
return r;
}
bool flag = false;
PdtExpToken? buf = null;
while (true) {
if (buf != null && t.Type != 0x0200) {
PdtExpression def;
if (defs.TryGetValue(buf.Value.Value, out def)) {
foreach (var i in def.Instructions) ins.AddLast(i);
}
else ins.AddLast(new PdtInstruction.PushVariable(buf.Value.Value));
buf = null;
TryPushAdjMul(ins, ref flag);
}
switch (t.Type) {
case 0x0020:
buf = t;
break;
case 0x0040:
float num = float.Parse(t.Value);
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.Number, BitConverter.GetBytes(num)));
break;
case 0x0100:
int strlen = t.Value.Length;
unsafe {
var strbuf = new byte[strlen * sizeof(char) + sizeof(int)];
fixed (byte* psuffix = strbuf) {
*(int*)psuffix = strlen;
}
Encoding.Unicode.GetBytes(t.Value, 0, strlen, strbuf, sizeof(int));
ins.AddLast(new PdtInstruction.PushConstant(PdtInternalType.String, strbuf));
}
break;
case 0x0200:
int pc;
InterpretExp(ins, -1, out pc);
if (buf != null) {
ins.AddLast(new PdtInstruction.Operate(buf.Value.Value, pc));
buf = null;
}
else if (pc > 1) {
ins.AddLast(new PdtInstruction.Operate(",", pc));
}
else if (pc == 0)
throw new FormatException("Empty subexpression");
break;
default:
return t;
}
if (buf == null) TryPushAdjMul(ins, ref flag);
t = GetToken();
}
}
void TryPushAdjMul(LinkedList<PdtInstruction> ins, ref bool flag) {
if (flag) ins.AddLast(new PdtInstruction.Operate("*", 2));
else flag = true;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: a551104b532c8d0469917e3e0e8c2cba
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,28 @@
namespace Cryville.Common.Pdt {
/// <summary>
/// The identifiers of the internal types of PDT.
/// </summary>
public static class PdtInternalType {
internal readonly static int Error = "error".GetHashCode();
/// <summary>
/// Array of a same variable-length type, with a suffix indicating the element count and the element type.
/// </summary>
public readonly static int Array = "array".GetHashCode();
/// <summary>
/// IEEE 754 32-bit floating-point number.
/// </summary>
public readonly static int Number = "number".GetHashCode();
/// <summary>
/// A sequence of UTF-16 code units, with a prefix indicating the number of the code units.
/// </summary>
public readonly static int String = "string".GetHashCode();
/// <summary>
/// A sequence of UTF-16 code units representing the name of an undefined variable.
/// </summary>
public readonly static int Undefined = "undefined".GetHashCode();
/// <summary>
/// Vector of a same constant-length type, with a suffix indicating the element type.
/// </summary>
public readonly static int Vector = "vector".GetHashCode();
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 36eb4aecae64aff4891c4ddd2d1ff68c
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,232 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Reflection;
using System.Text.RegularExpressions;
namespace Cryville.Common.Pdt {
/// <summary>
/// Interpreter for Property Definition Tree (PDT) file format.
/// </summary>
/// <typeparam name="T">The object type represented by the PDT.</typeparam>
public partial class PdtInterpreter<T> {
/// <summary>
/// The character category map.
/// </summary>
/// <remarks>
/// <list type="bullet">
/// <item><term><c>0x0001</c></term><description>White Space</description></item>
/// <item><term><c>0x0010</c></term><description>Identifier</description></item>
/// <item><term><c>0x0020</c></term><description>Identifier Begin</description></item>
/// <item><term><c>0x0040</c></term><description>Digit</description></item>
/// <item><term><c>0x0080</c></term><description>Operator</description></item>
/// <item><term><c>0x0100</c></term><description>String</description></item>
/// <item><term><c>0x0200</c></term><description>Opening Bracket</description></item>
/// <item><term><c>0x0400</c></term><description>Closing Bracket</description></item>
/// <item><term><c>0x0800</c></term><description>End of Expression</description></item>
/// <item><term><c>0x1000</c></term><description>End of Key</description></item>
/// </list>
/// </remarks>
readonly static int[] cm = new int[] {
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0001, 0x0001, 0x0000, 0x0000, 0x0001, 0x0000, 0x0000,
0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000,
0x0001, 0x0080, 0x0100, 0x0000, 0x0030, 0x0080, 0x0080, 0x0000, 0x0200, 0x0400, 0x0080, 0x0080, 0x0080, 0x0080, 0x0040, 0x0080,
0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x0050, 0x1000, 0x1800, 0x0080, 0x0080, 0x0080, 0x0000,
0x0080, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0000, 0x0000, 0x0000, 0x0000, 0x0030,
0x0000, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030,
0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x0030, 0x1000, 0x0080, 0x1000, 0x0000, 0x0000,
};
/// <summary>
/// Interprets a source string to an object of type <typeparamref name="T" />.
/// </summary>
/// <param name="src">The source string.</param>
/// <returns>The interpreted object.</returns>
public static T Interpret(string src) {
return Interpret(src, BinderAttribute.CreateBinderOfType(typeof(T)));
}
/// <summary>
/// Interprets a source string to an object of type <typeparamref name="T" /> with a binder.
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="binder">The binder.</param>
/// <returns>The interpreted object.</returns>
public static T Interpret(string src, Binder binder) {
return new PdtInterpreter<T>(src, binder).Interpret();
}
readonly string _src;
readonly Binder _binder;
protected int Position { get; private set; }
#pragma warning disable IDE1006
protected char cc { get { return _src[Position]; } }
protected int ct { get { return cm[cc]; } }
protected string tokenb(int flag) { // Token Whitelist
int sp = Position;
while ((ct & flag) == 0) Position++;
return _src.Substring(sp, Position - sp);
}
protected string tokenw(int flag) { // Token Whitelist
int sp = Position;
while ((ct & flag) != 0) Position++;
return _src.Substring(sp, Position - sp);
}
protected void ws() {
while ((ct & 0x0001) != 0) Position++;
}
#pragma warning restore IDE1006
protected char GetChar() {
char r = cc;
Position++;
return r;
}
protected string GetIdentifier() {
if ((ct & 0x0020) == 0) return "";
return tokenw(0x0010);
}
protected string GetNumber() {
return tokenw(0x0040);
}
protected string GetString() {
int sp = Position;
do {
if (cc == '\\') Position++;
Position++;
} while (ct != 0x0100);
Position++;
return Regex.Replace(_src.Substring(sp + 1, Position - sp - 2), @"\\(.)", "$1");
}
protected PdtExpression GetExp() {
var ins = new LinkedList<PdtInstruction>();
int _;
InterpretExp(ins, -2, out _);
return new PdtExpression(ins);
}
readonly Dictionary<string, PdtExpression> defs = new Dictionary<string, PdtExpression>();
/// <summary>
/// Creates an instance of the <see cref="PdtInterpreter{T}" /> class.
/// </summary>
/// <param name="src">The source string.</param>
/// <param name="binder">The binder. May be <c>null</c>.</param>
public PdtInterpreter(string src, Binder binder) {
_src = src;
_binder = binder;
if (_binder == null)
_binder = BinderAttribute.CreateBinderOfType(typeof(T));
}
/// <summary>
/// Interprets the source to an object of type <typeparamref name="T" />.
/// </summary>
/// <returns>The interpreted object.</returns>
public T Interpret() {
InterpretDirectives();
return (T)InterpretObject(typeof(T));
}
void InterpretDirectives() {
bool flag = false;
ws();
while (cc == '#') {
Position++;
switch (GetIdentifier()) {
case "ver":
ws();
Logger.Log("main", 3, "PDT", "Legacy PDT directive #ver={0} found. Ignoring.", GetNumber());
break;
case "format":
ws();
if (GetNumber() != "1")
throw new NotSupportedException("Format not supported");
flag = true;
break;
case "define":
if (!flag) throw new FormatException("Format directive not found");
ws();
string dname = GetIdentifier();
ws();
PdtExpression dexp = GetExp();
defs.Add(dname, dexp);
break;
default:
throw new NotSupportedException("Unsupported directive found");
}
ws();
}
if (!flag) throw new FormatException("Format directive not found");
}
object InterpretObject(Type type) {
var result = ReflectionHelper.InvokeEmptyConstructor(type);
bool dictflag = ReflectionHelper.IsGenericDictionary(type);
while (true) {
try { ws(); }
catch (IndexOutOfRangeException) { return result; }
object pkey = InterpretKey(type);
char c = GetChar();
switch (c) {
case '{':
if (dictflag) {
var ktype = type.GetGenericArguments()[0];
var ptype = type.GetGenericArguments()[1];
object key = _binder.ChangeType(pkey, ktype, null);
object value = InterpretObject(ptype);
((IDictionary)result).Add(key, value);
}
else {
MemberInfo prop;
bool flag = ReflectionHelper.TryFindMemberWithAttribute<ElementListAttribute>(type, out prop);
if (!flag && pkey is string) prop = ReflectionHelper.GetMember(type, (string)pkey);
Type ptype = ReflectionHelper.GetMemberType(prop);
if (ReflectionHelper.IsGenericDictionary(ptype)) {
var ktype = ptype.GetGenericArguments()[0];
var vtype = ptype.GetGenericArguments()[1];
if (flag) {
object key = _binder.ChangeType(pkey, ktype, null);
object value = InterpretObject(vtype);
((IDictionary)ReflectionHelper.GetValue(prop, result)).Add(key, value);
}
else ReflectionHelper.SetValue(prop, result, InterpretObject(ptype));
}
else ReflectionHelper.SetValue(prop, result, InterpretObject(ptype));
}
break;
case ':':
case ';':
var exp = c == ';' ? _emptyexp : GetExp();
if (dictflag) {
var ktype = type.GetGenericArguments()[0];
var vtype = type.GetGenericArguments()[1];
object key = _binder.ChangeType(pkey, ktype, null);
object value = _binder.ChangeType(exp, vtype, null);
((IDictionary)result).Add(key, value);
}
else {
MemberInfo prop;
bool flag = ReflectionHelper.TryFindMemberWithAttribute<PropertyListAttribute>(type, out prop);
if (!flag && pkey is string) prop = ReflectionHelper.GetMember(type, (string)pkey);
var ptype = ReflectionHelper.GetMemberType(prop);
if (!typeof(IDictionary).IsAssignableFrom(ptype)) {
object value = _binder.ChangeType(exp, ptype, null);
ReflectionHelper.SetValue(prop, result, value, _binder);
}
else {
var ktype = ptype.GetGenericArguments()[0];
var vtype = ptype.GetGenericArguments()[1];
object key = _binder.ChangeType(pkey, ktype, null);
object value = _binder.ChangeType(exp, vtype, null);
((IDictionary)ReflectionHelper.GetValue(prop, result)).Add(key, value);
}
}
break;
case '}':
return result;
}
}
}
protected virtual object InterpretKey(Type type) {
return tokenb(0x1000).Trim();
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 2a66a8564bee7c8449417a795cee8729
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,77 @@
using System;
namespace Cryville.Common.Pdt {
/// <summary>
/// PDT operator.
/// </summary>
public unsafe abstract class PdtOperator {
byte* _prmem;
int _loadindex;
readonly PdtVariableMemory[] _operands;
/// <summary>
/// The count of the operands loaded.
/// </summary>
protected int LoadedOperandCount { get { return ParamCount - _loadindex; } }
/// <summary>
/// Gets the operand at the specified index.
/// </summary>
/// <param name="index">The index.</param>
/// <returns>The operand at the specified index.</returns>
/// <exception cref="IndexOutOfRangeException"><paramref name="index" /> is not less than <see cref="LoadedOperandCount" /> or less than 0.</exception>
protected PdtVariableMemory GetOperand(int index) {
if (index >= LoadedOperandCount || index < 0) throw new IndexOutOfRangeException();
int i = index + _loadindex;
return _operands[i];
}
internal int ParamCount { get; private set; }
/// <summary>
/// Creates an instance of the <see cref="PdtOperator" /> class.
/// </summary>
/// <param name="pc">The suggested parameter count.</param>
protected PdtOperator(int pc) {
ParamCount = pc;
_operands = new PdtVariableMemory[pc];
}
PdtEvaluatorBase _etor;
bool _failure = false;
bool _rfreq = true;
internal void Begin(PdtEvaluatorBase etor) {
_etor = etor;
_failure = false;
_loadindex = ParamCount;
}
internal void LoadOperand(PdtVariableMemory mem) {
if (_loadindex == 0) return;
_operands[--_loadindex] = mem;
}
internal void Call(byte* prmem, bool noset) {
_prmem = prmem;
_rfreq = false;
Execute();
if (!_rfreq && !noset) throw new InvalidOperationException("Return frame not set");
if (_failure) {
if (_rfreq) _etor.DiscardStack();
throw new InvalidOperationException("Evaluation failed");
}
}
/// <summary>
/// Executes the operator.
/// </summary>
protected abstract void Execute();
/// <summary>
/// Gets a return frame.
/// </summary>
/// <param name="type">The type of the frame.</param>
/// <param name="len">The length of the frame.</param>
/// <returns>The return frame.</returns>
/// <exception cref="InvalidOperationException">The return frame has already been requested.</exception>
protected PdtVariableMemory GetReturnFrame(int type, int len) {
if (_rfreq) {
_failure = true;
throw new InvalidOperationException("Return frame already requested");
}
_rfreq = true;
return _etor.StackAlloc(type, _prmem, len);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: dc184c2d84fe5ea449cea78cba46d74d
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,749 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
namespace Cryville.Common.Pdt {
#if false
[Obsolete]
public static class PdtReader {
readonly static char[] ws = {
' ', '\t', '\n', '\r'
};
readonly static char[] spunc = {
' ', '\t', '\n', '\r',
'{', '}', ';'
};
readonly static char[] ipunc = {
' ', '\t', '\n', '\r',
'{', '}', ';',
':'
};
readonly static char[] vpunc = {
'{', '}', ';'
};
static int pos;
static string data;
static char cc {
get {
return data[pos];
}
}
static bool eof {
get {
return pos == data.Length;
}
}
static Dictionary<string, Expression> definitions;
public static T Read<T>(string _data, Binder binder = null) {
data = _data;
pos = 0;
definitions = new Dictionary<string, Expression>();
while (true) {
if (cc == '#') {
SkipChar();
var s = GetIdentifier();
switch (s) {
case "ver":
var s2 = GetString();
if (s2 != "1") throw new FormatException("Invalid PDT version");
continue;
case "define":
var s3 = GetString();
var s4 = GetValue();
definitions.Add(s3, new Expression(s4, definitions));
SkipChar();
// TODO
continue;
default:
SkipLine();
continue;
}
}
break;
}
if (binder == null)
binder = BinderAttribute.CreateBinderOfType(typeof(T));
return (T)ParseObject(typeof(T), binder);
}
static object ParseObject(Type type, Binder binder) {
// TODO Binder
object obj = type.GetConstructor(new Type[]{}).Invoke(new object[]{});
while (true) {
if (eof) return obj;
string str = GetValue();
if (cc == '{') { // List item
SkipChar();
string strkey = str;
if (typeof(IDictionary).IsAssignableFrom(type)) {
var ktype = type.GetGenericArguments()[0];
var ptype = type.GetGenericArguments()[1];
object key = binder.ChangeType(strkey, ktype, null);
object value = ParseObject(ptype, binder);
((IDictionary)obj).Add(key, value);
}
else {
MemberInfo prop = null;
Type ttype = null;
bool flag = ReflectionHelper.TryFindMemberWithAttribute<ElementListAttribute>(type, out prop);
if (!flag)
prop = ReflectionHelper.GetMember(type, strkey);
ttype = ReflectionHelper.GetMemberType(prop);
if (!typeof(IDictionary).IsAssignableFrom(ttype)) {
throw new NotImplementedException();
}
else {
var ktype = ttype.GetGenericArguments()[0];
var ptype = ttype.GetGenericArguments()[1];
if (flag) {
object key = binder.ChangeType(strkey, ktype, null);
object value = ParseObject(ptype, binder);
((IDictionary)ReflectionHelper.GetValue(prop, obj)).Add(key, value);
}
else {
ReflectionHelper.SetValue(prop, obj, ParseObject(ttype, binder));
}
}
}
}
else if (cc == ';') { // Single property
SkipChar();
string strkey;
MemberInfo prop = null;
if (str[0] == '*') { // Component-like property
strkey = str.Substring(1);
prop = ReflectionHelper.FindMemberWithAttribute<ComponentListAttribute>(type);
var ttype = ReflectionHelper.GetMemberType(prop);
if (!typeof(IList).IsAssignableFrom(ttype))
throw new Exception(); // TODO
var ktype = ttype.GetGenericArguments()[0];
object key = binder.ChangeType(strkey, ktype, null);
((IList)ReflectionHelper.GetValue(prop, obj)).Add(key);
}
else { // Common property
var kv = str.Split(new char[]{':'}, 2);
strkey = kv[0];
// TODO
if (typeof(IDictionary).IsAssignableFrom(type)) {
var ktype = type.GetGenericArguments()[0];
var ptype = type.GetGenericArguments()[1];
object key = binder.ChangeType(strkey, ktype, null);
object value = binder.ChangeType(new Expression(
kv.Length == 1 ? "true" : kv[1], definitions
), ptype, null);
((IDictionary)obj).Add(key, value);
}
else {
bool flag = ReflectionHelper.TryFindMemberWithAttribute<PropertyListAttribute>(type, out prop);
if (!flag)
prop = ReflectionHelper.GetMember(type, strkey);
var ttype = ReflectionHelper.GetMemberType(prop);
if (!typeof(IDictionary).IsAssignableFrom(ttype)) {
object value = binder.ChangeType(new Expression(
kv.Length == 1 ? "true" : kv[1], definitions
), ttype, null);
ReflectionHelper.SetValue(prop, obj, value, binder);
}
else {
var ktype = ttype.GetGenericArguments()[0];
var ptype = ttype.GetGenericArguments()[1];
object key = binder.ChangeType(strkey, ktype, null);
object value = binder.ChangeType(new Expression(
kv.Length == 1 ? "true" : kv[1], definitions
), ptype, null);
((IDictionary)ReflectionHelper.GetValue(prop, obj)).Add(key, value);
}
}
}
}
else if (cc == '}') {
try { SkipChar(); }
catch (IndexOutOfRangeException) { }
return obj;
}
else throw new Exception(); // TODO
}
}
static void SkipChar() {
pos++;
SkipWs();
}
static void SkipWs() {
while (true) {
for (; ws.Contains(cc); pos++);
if (data[pos] == '/' && data[pos + 1] == '*') {
for (; data[pos] != '*' || data[pos+1] != '/'; pos++);
pos += 2;
}
else return;
}
}
static string GetIdentifier() {
SkipWs();
string r = "";
for (; !ipunc.Contains(cc); pos++) r += cc;
SkipWs();
return r;
}
static string GetString() {
SkipWs();
string r = "";
for (; !spunc.Contains(cc); pos++) r += cc;
SkipWs();
return r;
}
static string GetValue() {
SkipWs();
string r = "";
for (; !vpunc.Contains(cc); pos++) r += cc;
SkipWs();
return r.Trim();
}
static void SkipLine() {
for (; cc != '\n'; pos++);
SkipWs();
}
}
#endif
public class ElementListAttribute : Attribute { }
public class ComponentListAttribute : Attribute { }
public class PropertyListAttribute : Attribute { }
#if false
[Obsolete]
public abstract class ExpBase {
public string exp {
get;
private set;
}
public ExpBase(string s) {
exp = s;
}
public override string ToString() {
return exp;
}
}
[Obsolete]
public abstract class ValueBase : ExpBase {
public ValueBase(string s) : base(s) { }
object preEvalResult;
byte preEvalDepth = 0;
public bool IsDynamic {
get { return preEvalDepth == 0; }
}
protected abstract object EvalInternal(IEvaluator etor);
public void PreEval(IEvaluator etor, byte depth = 1) {
if (depth == 0) throw new ArgumentException("depth cannot be 0");
if (preEvalDepth != 0 && preEvalDepth < depth) return;
try {
preEvalResult = PreEvalInternal(etor, depth);
preEvalDepth = depth;
}
catch (Exception) { }
}
protected virtual object PreEvalInternal(IEvaluator etor, byte depth) {
return Eval(etor);
}
public object Eval(IEvaluator etor) {
if (preEvalDepth != 0)
return preEvalResult;
return EvalInternal(etor);
}
}
[Obsolete]
public class Identifier : ValueBase {
public Identifier(string s) : base(s) { }
protected override object EvalInternal(IEvaluator etor) {
return etor.EvalIdentifier(exp);
}
}
[Obsolete]
public class Operand : ExpBase {
public OperandPriority Priority {
get;
private set;
}
public Operand(string s) : base(s) {
switch (s) {
case ".": Priority = OperandPriority.Prop; break;
case "*": case "/": Priority = OperandPriority.Mul; break;
case "+": case "-": Priority = OperandPriority.Add; break;
case " ": case ",": Priority = OperandPriority.Sep; break;
default: Priority = OperandPriority.None; break;
}
}
public Operand(string s, OperandPriority p) : base(s) {
Priority = p;
}
}
[Obsolete]
public enum OperandPriority {
None = 0,
Prop = 5,
NeibMul = 4,
Mul = 3,
Add = 2,
Sep = 1
}
[Obsolete]
public abstract class ConstantBase : ValueBase {
public ConstantBase(string s) : base(s) { }
}
[Obsolete]
public class CNumber : ConstantBase {
public CNumber(string s) : base(s) { }
protected override object EvalInternal(IEvaluator etor) {
return etor.ParseNumber(exp);
}
}
[Obsolete]
public class CString : ConstantBase {
public CString(string s) : base(s) { }
protected override object EvalInternal(IEvaluator etor) {
return etor.ParseString(exp);
}
}
[Obsolete]
public class BracketInitial : ExpBase {
public BracketInitial() : base("(") { }
}
[Obsolete]
public class BracketFinal : ExpBase {
public BracketFinal() : base(")") { }
}
[Obsolete]
public class Expression : ValueBase {
List<ValueBase> estack = new List<ValueBase>();
List<Operand> ostack = new List<Operand>();
StackType type;
enum StackType {
Root, Unary, Bracketed, Supportive, Function
}
readonly OperandPriority Priority;
public Expression(string s, Dictionary<string, Expression> def) : base("") {
var exp = s.Trim();
type = StackType.Root;
int pos = 0;
var b = new List<ExpBase>();
while (pos < exp.Length) {
b.Add(Forward(ref exp, ref pos));
}
var lb = b[b.Count - 1];
if (def.ContainsKey(lb.exp)) {
b.Add(def[lb.exp]);
b.Remove(lb);
}
for (int i = b.Count - 2; i >= 0; i--) {
// TODO Insertion
var lhb = b[i];
var rhb = b[i + 1];
if (lhb is ConstantBase || lhb is Expression || lhb is BracketFinal) {
if (rhb is Identifier || rhb is Expression || rhb is BracketInitial) {
b.Insert(i + 1, new Operand("*", OperandPriority.NeibMul));
}
}
else if (lhb is Identifier) {
if (rhb is Expression) {
b.Insert(i + 1, new Operand("*", OperandPriority.NeibMul));
}
if (def.ContainsKey(lhb.exp)) {
b.Insert(i, def[lhb.exp]);
b.Remove(lhb);
}
}
}
int p = 0;
estack.Add(new Expression(b, ref p, StackType.Bracketed, def));
}
public Expression Clone() {
var r = (Expression)this.MemberwiseClone();
var es = new ValueBase[estack.Count];
estack.CopyTo(es);
r.estack = es.ToList();
var os = new Operand[ostack.Count];
ostack.CopyTo(os);
r.ostack = os.ToList();
return r;
}
Expression(List<ExpBase> b, ref int p, StackType t, Dictionary<string, Expression> def) : base("") {
type = t;
if (t == StackType.Unary) {
ostack.Add((Operand)b[p]);
p++;
}
while (p < b.Count - 1) {
if (estack.Count == 0) {
var b0 = b[p];
if (b0 is Operand) {
var lp = p;
var e = new Expression(
b,
ref p,
StackType.Unary,
def
);
b.Insert(p, e);
b.RemoveRange(lp, p - lp);
p = lp;
}
else if (b0 is BracketInitial) {
var lp = p;
p++;
var e = new Expression(
b,
ref p,
StackType.Bracketed,
def
);
b.Insert(p, e);
b.RemoveRange(lp, p - lp);
p = lp;
}
estack.Add((ValueBase)b[p]);
p++;
if (t == StackType.Unary) {
if (estack.Count != 1)
throw new Exception(); // TODO
else return;
}
}
if (p >= b.Count) return;
var b1 = b[p];
if (b1 is BracketFinal) {
if (t == StackType.Bracketed) p++;
return;
}
var b2 = b[p + 1];
if (b2 is BracketInitial) {
var lp = p + 1;
p += 2;
var e = new Expression(
b,
ref p,
StackType.Bracketed,
def
);
b.Insert(p, e);
b.RemoveRange(lp, p - lp);
p = lp - 1;
b2 = b[p + 1];
}
if (b1 is Operand) {
if (estack.Count == 1)
Priority = ((Operand)b1).Priority;
if (b2 is Operand) {
var lp = p + 1;
p++;
var e = new Expression(
b,
ref p,
StackType.Unary,
def
);
b.Insert(p, e);
b.RemoveRange(lp, p - lp);
p = lp - 1;
b2 = b[p + 1];
}
if (p + 2 >= b.Count) {
ostack.Add((Operand)b1);
estack.Add((ValueBase)b2);
p += 2;
return;
}
var b3 = b[p + 2];
if (b3 is BracketFinal) {
ostack.Add((Operand)b1);
estack.Add((ValueBase)b2);
p += 2;
if (t == StackType.Bracketed) p++;
return;
}
else if (b3 is Operand) {
var o1 = (Operand)b1; var o2 = (Operand)b3;
if (o2.Priority == Priority) {
ostack.Add(o1); estack.Add((ValueBase)b2);
p += 2;
continue;
}
else if (o2.Priority > Priority) {
var lp = p + 1;
p++;
var e = new Expression(
b, ref p,
StackType.Supportive, def
);
b.Insert(p, e);
b.RemoveRange(lp, p - lp);
p = lp - 1;
continue;
}
else if (o2.Priority < Priority) {
ostack.Add(o1);
estack.Add((ValueBase)b2);
// b.RemoveRange(0, 2);
if (type == StackType.Bracketed) {
Expression cl = this.Clone();
cl.type = StackType.Supportive;
estack.Clear();
ostack.Clear();
estack.Add(cl);
Priority = o2.Priority;
p += 2;
continue;
}
else {
type = StackType.Supportive;
p += 2;
return;
}
}
}
else
throw new Exception(); // TODO
}
else
throw new Exception(); // TODO
throw new Exception(); // TODO
/*if (lb is Identifier) {
if (def.ContainsKey(lb.exp)) {
b.Add(def[lb.exp]);
b.Remove(lb);
}
}
// Unary
if (estack.Count == 0) {
if (b[0] is Operand) {
b.Add(new Expression(
b,
ref p,
StackType.Unary,
def
));
b.RemoveAt(0);
}
if (b[0] is ValueBase) {
estack.Add((ValueBase)b[0]);
b.RemoveAt(0);
if (type == StackType.Unary) return;
if (b.Count == 0) continue;
}
}
if (estack.Count == 1) {
if (b[0] is Operand)
Priority = ((Operand)b[0]).Priority;
}
// Bracket
if (lb is BracketInitial) {
b.Remove(lb);
b.Add(new Expression(b, ref p, StackType.Bracketed, def));
}
else if (lb is BracketFinal) {
if (type != StackType.Bracketed) p--;
foreach (var i in b) {
if (i is Operand) ostack.Add((Operand)i);
else if (i is BracketFinal) return;
else estack.Add((ValueBase)i);
}
}
var c = b.Count;
if (c <= 1) continue;
// Two blocks
lb = b[c - 1];
var lb1 = b[c - 2];
if (lb is Operand && lb1 is Operand) {
b.Add(new Expression(
b,
ref p,
StackType.Unary,
def
));
b.RemoveAt(b.Count - 2);
}
c = b.Count;
if (c <= 2) continue;
// Three blocks
var b0 = b[0];
var b1 = b[1];
var b2 = b[2];
if (!(b0 is Operand))
throw new Exception(); // TODO
if (!(b1 is ValueBase))
throw new Exception(); // TODO
if (!(b2 is Operand))
throw new Exception(); // TODO
var o1 = (Operand)b0; var o2 = (Operand)b2;
if (o2.Priority == Priority) {
ostack.Add(o1); estack.Add((ValueBase)b1);
b.Remove(o1); b.Remove(b1);
continue;
}
if (o2.Priority > Priority) {
b.Add(new Expression(
b, ref p,
StackType.Supportive, def
));
b.RemoveRange(1, b.Count - 2);
continue;
}
if (o2.Priority < Priority) {
ostack.Add(o1);
estack.Add((ValueBase)b1);
b.RemoveRange(0, 2);
if (type == StackType.Bracketed) {
Expression cl = this.Clone();
cl.type = StackType.Supportive;
estack.Clear();
ostack.Clear();
estack.Add(cl);
Priority = o2.Priority;
}
else {
type = StackType.Supportive;
// p = o2.Index;
return;
}
}*/
}
estack.Add((ValueBase)b[b.Count - 1]);
p++;
}
ExpBase Forward(ref string s, ref int p) {
char sc = s[p];
string r = "";
if (cat(sc) == 3) {
p++;
return new BracketInitial();
}
else if (cat(sc) == 4) {
p++;
return new BracketFinal();
}
else if (cat(sc) == 5) {
for (; p < s.Length; p++) {
if (cat(s[p]) != 5) break;
r += s[p];
}
if (r == ".") return new Operand(r);
else return new CNumber(r);
}
else if (cat(sc) == 2) {
p++;
for (; s[p] != sc; p++)
r += s[p];
p++;
return new CString(r);
}
else if (cat(sc) == 0) {
for (; p < s.Length; p++) {
if (cat(s[p]) != 0) break;
r += s[p];
}
if (p == s.Length) return new Identifier(r);
if (s[p] == '(') return new Operand(r);
else return new Identifier(r);
}
else if (cat(sc) == 1) {
p++;
return new Operand(sc.ToString());
}
else
throw new Exception(); // TODO
}
protected override object EvalInternal(IEvaluator etor) {
if (type == StackType.Unary) {
if (ostack[0].Priority == OperandPriority.None)
return etor.OperateFunction(ostack[0], estack[0].Eval(etor));
else
return etor.OperateUnary(ostack[0], estack[0].Eval(etor));
}
else {
object r = estack[0].Eval(etor);
for (int i = 0; i < ostack.Count; i++) {
r = etor.OperateBinary(
ostack[i], r, estack[i+1].Eval(etor)
);
}
return r;
}
}
public T Eval<T>(IEvaluator etor) {
return (T)etor.Cast(typeof(T), EvalInternal(etor));
}
protected override object PreEvalInternal(IEvaluator etor, byte depth) {
try { return EvalInternal(etor); }
catch (Exception) {
foreach (var v in estack)
v.PreEval(etor, depth);
throw;
}
}
// 0: Other, 1: Operand, 2: String,
// 3: SOE, 4: EOE, 5: Number
static readonly byte[] ctl = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
1, 1, 2, 1, 0, 1, 1, 2, 3, 4, 1, 1, 1, 1, 5, 1,
5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 1, 1, 1, 1, 1, 1,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
};
static byte cat(char c) {
if (c >> 7 != 0)
return 0;
else
return ctl[c];
}
public override string ToString() {
if (type == StackType.Unary)
return string.Format("{0}{1}", ostack[0], estack[0]);
if (type == StackType.Function)
return string.Format("{0}{1}", ostack[0], estack[0]);
string r = estack[0].ToString();
for (int i = 0; i < ostack.Count; i++) {
r += ostack[i].ToString();
r += estack[i + 1].ToString();
}
if (type == StackType.Bracketed)
r = string.Format("({0})", r);
return r;
}
}
[Obsolete]
public interface IEvaluator {
object ParseNumber(string exp);
object ParseString(string exp);
object EvalIdentifier(string exp);
object OperateUnary(Operand op, object q);
object OperateBinary(Operand op, object q, object r);
object OperateFunction(Operand op, object q);
// TODO [Obsolete]
object Cast(Type type, object obj);
//public abstract Func<Type, object, object> GetCastCallback(Type dest, Type source);
}
#endif
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 688e46fb4d8e6a041844f592f1333af7
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,143 @@
using System;
using System.Text;
using UnityEditor;
namespace Cryville.Common.Pdt {
/// <summary>
/// Span on the memory of a <see cref="PdtEvaluatorBase" />.
/// </summary>
public unsafe struct PdtVariableMemory {
byte* _ptr;
/// <summary>
/// The length of the span.
/// </summary>
public int Length { get; private set; }
/// <summary>
/// The type of the span.
/// </summary>
public int Type { get; private set; }
internal PdtVariableMemory(int type, byte* ptr, int len) {
Type = type;
_ptr = ptr;
Length = len;
}
/// <summary>
/// Copies the memory in the span to a buffer.
/// </summary>
/// <param name="dest">The destination buffer.</param>
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
public void CopyTo(byte[] dest, int destOffset) {
fixed (byte* ptr = dest) {
CopyTo(ptr, destOffset, Length);
}
}
/// <summary>
/// Copies the memory in the span to a buffer.
/// </summary>
/// <param name="dest">The destination buffer.</param>
/// <param name="destOffset">The offset on the destination buffer to start copying to.</param>
/// <param name="length">The length to copy.</param>
public void CopyTo(byte* dest, int destOffset, int length) {
for (int i = 0; i < length; i++)
dest[destOffset + i] = _ptr[i];
}
/// <summary>
/// Gets the memory of the span as a number.
/// </summary>
/// <returns>A number.</returns>
/// <exception cref="InvalidCastException">The span does not represent a number.</exception>
public float AsNumber() {
if (Type != PdtInternalType.Number)
throw new InvalidCastException("Not a number");
float value;
byte* ptr = (byte*)&value;
for (int i = 0; i < sizeof(float); i++)
ptr[i] = _ptr[i];
return value;
}
/// <summary>
/// Sets the memory of the span to a number.
/// </summary>
/// <param name="value">The number.</param>
/// <exception cref="InvalidCastException">The span does not represent a number.</exception>
public void SetNumber(float value) {
if (Type != PdtInternalType.Number)
throw new InvalidCastException("Not a number");
byte* ptr = (byte*)&value;
for (int i = 0; i < sizeof(float); i++)
_ptr[i] = ptr[i];
}
/// <summary>
/// Gets the memory of the span as a string.
/// </summary>
/// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>A string.</returns>
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
public string AsString(int offset = 0) {
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
throw new InvalidCastException("Not a string");
var len = *(int*)(_ptr + offset);
return new string((char*)(_ptr + offset + sizeof(int)), 0, len);
}
/// <summary>
/// Sets the memory of the span to a string.
/// </summary>
/// <param name="value">The string.</param>
/// <param name="offset">The offset from the start of the span.</param>
/// <exception cref="InvalidCastException">The span at the offset does not represent a string.</exception>
/// <exception cref="InvalidOperationException">The length of the span is not sufficient.</exception>
public void SetString(string value, int offset = 0) {
if (Type != PdtInternalType.String && Type != PdtInternalType.Array)
throw new InvalidCastException("Not a string");
int strlen = value.Length;
if (Length < strlen * sizeof(char) + sizeof(int) + offset)
throw new InvalidOperationException("Frame length not sufficient");
char* ptr = (char*)(_ptr + offset + sizeof(int));
*(int*)(_ptr + offset) = strlen;
int i = 0;
foreach (var c in value) ptr[i++] = c;
}
/// <summary>
/// Gets the memory of the span as an undefined identifier.
/// </summary>
/// <param name="offset">The offset on the span to start reading from.</param>
/// <returns>The name of an undefined identifier.</returns>
/// <exception cref="InvalidCastException">The span does not represent an undefined identifier.</exception>
public string AsIdentifier(int offset = 0) {
if (Type != PdtInternalType.Undefined && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an identifier");
var len = *(int*)(_ptr + offset);
return new string((char*)(_ptr + offset + sizeof(int)), 0, len);
}
internal void* TrustedAsOfLength(int len) {
if (Length < len)
throw new InvalidCastException("Type not matched");
return _ptr;
}
/// <summary>
/// Gets the array suffix.
/// </summary>
/// <param name="arrtype">The type of the array.</param>
/// <param name="pc">The item count of the array.</param>
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
public void GetArraySuffix(out int arrtype, out int pc) {
if (Type != PdtInternalType.Array && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an array or vector");
arrtype = *(int*)(_ptr + Length - sizeof(int));
if (Type == PdtInternalType.Array) pc = *(int*)(_ptr + Length - 2 * sizeof(int));
else pc = -1;
}
/// <summary>
/// Sets the array suffix.
/// </summary>
/// <param name="arrtype">The type of the array.</param>
/// <param name="pc">The item count of the array.</param>
/// <exception cref="InvalidCastException">The span does not represent an array.</exception>
public void SetArraySuffix(int arrtype, int pc = 0) {
if (Type != PdtInternalType.Vector && Type != PdtInternalType.Array)
throw new InvalidCastException("Not an array or vector");
*(int*)(_ptr + Length - sizeof(int)) = arrtype;
if (Type == PdtInternalType.Array) *(int*)(_ptr + Length - 2 * sizeof(int)) = pc;
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 0e710eef537a1ac488ef6bce16625b62
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 925f95cb7c6644a4695b2701d42e1ea2
folderAsset: yes
timeCreated: 1606989037
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,50 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
namespace Cryville.Common.Plist {
public class PlistConvert {
public static T Deserialize<T>(string file) {
return (T)Deserialize(typeof(T), PlistCS.Plist.readPlist(file));
}
public static object Deserialize(Type type, object obj, Binder binder = null) {
if (binder == null)
binder = BinderAttribute.CreateBinderOfType(type);
if (obj is IList) {
var lobj = (List<object>)obj;
foreach (var i in lobj) {
throw new NotImplementedException(); // TODO
}
}
else if (obj is IDictionary) {
var dobj = (Dictionary<string, object>)obj;
if (typeof(IDictionary).IsAssignableFrom(type)) {
var result = (IDictionary)ReflectionHelper.InvokeEmptyConstructor(type);
var it = type.GetGenericArguments()[1];
foreach (var i in dobj) {
var value = Deserialize(it, i.Value, binder);
result.Add(i.Key, value);
}
return result;
}
else {
var result = ReflectionHelper.InvokeEmptyConstructor(type);
foreach (var i in dobj) {
var imis = type.GetMember(i.Key);
if (imis.Length == 0) continue;
var imi = imis[0];
var it = ReflectionHelper.GetMemberType(imi);
var value = Deserialize(it, i.Value, binder);
ReflectionHelper.SetValue(imi, result, value, binder);
}
return result;
}
}
else return obj;
throw new Exception(); // TODO
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 3bc71e8b62d4022409aa5518bbf3a7d8
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,102 @@
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
namespace Cryville.Common {
public static class ReflectionHelper {
static readonly Type[] emptyTypeArray = {};
public static ConstructorInfo GetEmptyConstructor(Type type) {
return type.GetConstructor(emptyTypeArray);
}
static readonly object[] emptyObjectArray = {};
public static object InvokeEmptyConstructor(Type type) {
return GetEmptyConstructor(type).Invoke(emptyObjectArray);
}
public static bool TryFindMemberWithAttribute<T>(Type t, out MemberInfo mi) where T : Attribute {
try {
mi = FindMemberWithAttribute<T>(t);
return true;
}
catch (MissingMemberException) {
mi = null;
return false;
}
}
public static MemberInfo FindMemberWithAttribute<T>(Type type) where T : Attribute {
var mil = type.FindMembers(
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static,
(m, o) => m.GetCustomAttributes(typeof(T), true).Length != 0,
null
);
if (mil.Length != 1)
throw new MissingMemberException(type.Name, typeof(T).Name);
return mil[0];
}
public static bool IsGenericDictionary(Type type) {
return type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary<,>);
}
public static MemberInfo GetMember(Type type, string name) {
var mil = type.GetMember(
name,
MemberTypes.Field | MemberTypes.Property,
BindingFlags.Public | BindingFlags.Instance
);
if (mil.Length != 1)
throw new MissingMemberException(type.Name, name);
return mil[0];
}
public static Type GetMemberType(MemberInfo mi) {
if (mi is FieldInfo)
return ((FieldInfo)mi).FieldType;
if (mi is PropertyInfo)
return ((PropertyInfo)mi).PropertyType;
else
throw new ArgumentException();
}
public static object GetValue(MemberInfo mi, object obj) {
if (mi is FieldInfo)
return ((FieldInfo)mi).GetValue(obj);
else if (mi is PropertyInfo)
return ((PropertyInfo)mi).GetValue(obj, new object[]{});
else
throw new ArgumentException();
}
public static void SetValue(MemberInfo mi, object obj, object value, Binder binder = null) {
if (mi is FieldInfo)
((FieldInfo)mi).SetValue(obj, value, BindingFlags.Default, binder, null);
else if (mi is PropertyInfo)
((PropertyInfo)mi).SetValue(obj, value, BindingFlags.Default, binder, emptyObjectArray, null);
else
throw new ArgumentException();
}
public static Type[] GetSubclassesOf<T>() where T : class {
var assemblies = AppDomain.CurrentDomain.GetAssemblies();
IEnumerable<Type> r = new List<Type>();
foreach (var a in assemblies)
r = r.Concat(a.GetTypes().Where(
t => t.IsClass
&& !t.IsAbstract
&& t.IsSubclassOf(typeof(T))
));
return r.ToArray();
}
public static string GetSimpleName(Type type) {
string result = type.Name;
var typeargs = type.GetGenericArguments();
if (typeargs.Length > 0) {
result = string.Format("{0}[{1}]", result, string.Join(",", from a in typeargs select GetSimpleName(a)));
}
return result;
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 16857d076fc8d534788424e6c6d180e0
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,23 @@
using System;
using System.Text;
namespace Cryville.Common {
public static class StringUtils {
public static string TrimExt(string s) {
return s.Substring(0, s.LastIndexOf("."));
}
public static string ToString(this TimeSpan timeSpan, int digits) {
var b = new StringBuilder();
bool flag = false;
if (timeSpan.TotalDays >= 1) {
flag = true;
b.Append(timeSpan.Days.ToString() + ":");
}
if (flag || timeSpan.TotalHours >= 1)
b.Append((timeSpan.Hours % 24).ToString() + ":");
b.Append((timeSpan.Minutes % 60).ToString("00") + ":");
b.Append((timeSpan.TotalSeconds % 60).ToString("00." + new string('0', digits)));
return b.ToString();
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9c45da9ea5560c84b9ecfb5a1161fb77
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 2debd5a8bd19dea4f9e8b0aab176881d
folderAsset: yes
timeCreated: 1605323649
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,34 @@
using UnityEngine;
using System;
namespace Cryville.Common.Unity {
static class CallHelper {
public static bool HasFlag(this Enum obj, Enum flag) {
ulong num = Convert.ToUInt64(flag);
ulong num2 = Convert.ToUInt64(obj);
return (num2 & num) == num;
}
public static void ShowException(Exception ex) {
ShowMessageBox(ex.ToString());
}
public static void ShowMessageBox(string message) {
GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Popup")).GetComponent<Popup>().Message = message;
}
public static void Purge(Transform obj) {
foreach (Transform i in obj)
GameObject.Destroy(i.gameObject);
}
/*public static void DownloadAndUnzip(string url, FileInfo file) {
using (DownloadDialog d = new DownloadDialog()) {
d.Download(url, file);
}
using (ZipFile z = new ZipFile(file.FullName)) {
z.ExtractAll(file.DirectoryName, ExtractExistingFileAction.OverwriteSilently);
}
}*/
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 98b3d4b7cc1ce054598780159356da35
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,142 @@
using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
namespace Cryville.Common.Unity {
public class FileDialog : MonoBehaviour {
Transform panel;
Transform title;
Transform drives;
Transform dirs;
Transform files;
public Action Callback { private get; set; }
#if UNITY_ANDROID && !UNITY_EDITOR_WIN
string androidStorage = "";
#endif
string fileName = "";
public string FileName {
get { return fileName; }
}
public string[] m_filter = new string[]{};
public string[] Filter {
set { m_filter = value; }
}
#pragma warning disable IDE0051
void Start() {
panel = gameObject.transform.Find("Panel");
title = panel.Find("Title/Text");
drives = panel.Find("Drives/DrivesInner");
dirs = panel.Find("Directories/DirectoriesInner");
files = panel.Find("Files/FilesInner");
if (CurrentDirectory == null) {
#if UNITY_EDITOR_WIN || UNITY_STANDALONE_WIN
CurrentDirectory = new DirectoryInfo(Directory.GetCurrentDirectory());
#elif UNITY_ANDROID
using (AndroidJavaClass ajc=new AndroidJavaClass("android.os.Environment"))
using (AndroidJavaObject file=ajc.CallStatic<AndroidJavaObject>("getExternalStorageDirectory")) {
androidStorage = file.Call<string>("getAbsolutePath");
CurrentDirectory = new DirectoryInfo(androidStorage);
}
#else
#error No default directory
#endif
}
UpdateGUI();
}
#pragma warning restore IDE0051
public void Show() {
fileName = null;
gameObject.SetActive(true);
}
public void Close() {
if (Callback != null) Callback.Invoke();
gameObject.SetActive(false);
}
public DirectoryInfo CurrentDirectory;
void OnDriveChanged(string s) {
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
CurrentDirectory = new DirectoryInfo(s);
#elif UNITY_ANDROID
switch (s) {
case "?storage":
CurrentDirectory = new DirectoryInfo(androidStorage);
break;
}
#else
#error No change drive logic
#endif
UpdateGUI();
}
void OnDirectoryChanged(string s) {
CurrentDirectory = new DirectoryInfo(CurrentDirectory.FullName + "/" + s);
UpdateGUI();
}
void OnFileChanged(string s) {
fileName = s;
Close();
}
void UpdateGUI() {
title.GetComponent<Text>().text = CurrentDirectory.FullName;
CallHelper.Purge(drives);
#if UNITY_STANDALONE_WIN || UNITY_EDITOR_WIN
var dl = Directory.GetLogicalDrives();
foreach (string d in dl) {
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
btn.GetComponentInChildren<Text>().text = d;
var ts = d;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged(ts));
btn.transform.SetParent(drives, false);
}
#elif UNITY_ANDROID
GameObject sbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
sbtn.GetComponentInChildren<Text>().text = "Storage";
sbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDriveChanged("?storage"));
sbtn.transform.SetParent(drives, false);
#else
#error No update GUI logic
#endif
CallHelper.Purge(dirs);
DirectoryInfo[] subdirs = CurrentDirectory.GetDirectories();
GameObject pbtn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
pbtn.GetComponentInChildren<Text>().text = "..";
pbtn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged(".."));
pbtn.transform.SetParent(dirs, false);
foreach (DirectoryInfo d in subdirs) {
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
btn.GetComponentInChildren<Text>().text = d.Name;
var ts = d.Name;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnDirectoryChanged(ts));
btn.transform.SetParent(dirs, false);
}
CallHelper.Purge(files);
FileInfo[] fl = CurrentDirectory.GetFiles();
foreach (FileInfo d in fl) {
foreach (string ext in m_filter)
if (d.Extension == ext) goto ext_matched;
continue;
ext_matched:
GameObject btn = GameObject.Instantiate<GameObject>(Resources.Load<GameObject>("Common/Button"));
btn.GetComponentInChildren<Text>().text = d.Name + " / " + (d.Length / 1024.0).ToString("0.0 KiB");
var ts = d.FullName;
btn.GetComponentInChildren<Button>().onClick.AddListener(() => OnFileChanged(ts));
btn.transform.SetParent(files, false);
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 9865f498871e30548959e6b28f91feae
timeCreated: 1608801352
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,9 @@
fileFormatVersion: 2
guid: 7548d5a078795b04b8c54524389ba0fe
folderAsset: yes
timeCreated: 1611035780
licenseType: Free
DefaultImporter:
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,102 @@
using System;
using UnityEngine;
namespace Cryville.Common.Unity.Input {
public delegate void InputEventDelegate(InputIdentifier id, InputVector vec);
public abstract class InputHandler : IDisposable {
public InputEventDelegate Callback { private get; set; }
~InputHandler() {
Dispose(false);
}
public void Dispose() {
Dispose(true);
GC.SuppressFinalize(this);
}
public abstract void Activate();
public abstract void Deactivate();
public abstract void Dispose(bool disposing);
public abstract bool IsNullable(int type);
public abstract byte GetDimension(int type);
public abstract string GetTypeName(int type);
public abstract double GetCurrentTimestamp();
protected void OnInput(int type, int id, InputVector vec) {
if (Callback != null) Callback(new InputIdentifier { Source = new InputSource { Handler = this, Type = type }, Id = id }, vec);
}
}
public struct InputSource : IEquatable<InputSource> {
public InputHandler Handler { get; set; }
public int Type { get; set; }
public override bool Equals(object obj) {
if (obj == null || !(obj is InputSource)) return false;
return Equals((InputSource)obj);
}
public bool Equals(InputSource other) {
return Handler == other.Handler && Type == other.Type;
}
public override int GetHashCode() {
return Handler.GetHashCode() ^ Type;
}
public static bool operator ==(InputSource lhs, InputSource rhs) {
return lhs.Equals(rhs);
}
public static bool operator !=(InputSource lhs, InputSource rhs) {
return !lhs.Equals(rhs);
}
}
public struct InputIdentifier : IEquatable<InputIdentifier> {
public InputSource Source { get; set; }
public int Id { get; set; }
public override bool Equals(object obj) {
if (obj == null || !(obj is InputIdentifier)) return false;
return Equals((InputIdentifier)obj);
}
public bool Equals(InputIdentifier other) {
return Source == other.Source && Id == other.Id;
}
public override int GetHashCode() {
return Source.GetHashCode() ^ ((Id << 16) | (Id >> 16));
}
public override string ToString() {
return string.Format("{0},{1}", Source, Id);
}
public static bool operator ==(InputIdentifier lhs, InputIdentifier rhs) {
return lhs.Equals(rhs);
}
public static bool operator !=(InputIdentifier lhs, InputIdentifier rhs) {
return !lhs.Equals(rhs);
}
}
public struct InputEvent {
public InputIdentifier Id { get; set; }
public InputVector From { get; set; }
public InputVector To { get; set; }
public override string ToString() {
return string.Format("[{0}] {1} -> {2}", Id, From, To);
}
}
public struct InputVector {
public double Time { get; set; }
public bool IsNull { get; set; }
public Vector3 Vector { get; set; }
public InputVector(double time) {
Time = time;
IsNull = true;
Vector = default(Vector3);
}
public InputVector(double time, Vector3 vector) {
Time = time;
IsNull = false;
Vector = vector;
}
public override string ToString() {
if (IsNull) return string.Format("null@{0}", Time);
else return string.Format("{0}@{1}", Vector.ToString("G9"), Time);
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 139cf0675329c9d46b902249fecdb500
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,194 @@
using System;
using System.Collections.Generic;
using System.Reflection;
namespace Cryville.Common.Unity.Input {
/*public class InputManager {
int newId = 0;
int GetNewId() {
newId++;
return newId;
}
PointerHandler ptrHandler;
readonly Queue<PointerInfo> ptrEv = new Queue<PointerInfo>();
readonly Dictionary<int, PointerInfo> ptr
= new Dictionary<int, PointerInfo>();
double originTime;
void Callback(int id, PointerInfo info) {
/*if (info.Phase != PointerPhase.Update)
Logger.Log(
"main", 0, "Input",
"[{0}, p{1}] {2} {3} {4} at {5} (cs:{6}, ori:{7}, prs:{8})",
info.EventTime, info.ProcessTime, info.Type,
id, info.Phase, info.Position,
info.ContactSize, info.Orientation, info.Pressure
);*
lock (ptrEv) {
ptrEv.Enqueue(info);
ptr[id] = info;
}
}
public void Init() {
try {
if (Environment.OSVersion.Platform == PlatformID.Win32NT) {
if (WindowsPointerHandler.IsSupported()) {
ptrHandler = new WindowsPointerHandler(true, GetNewId, Callback);
Logger.Log("main", 1, "Input", "Initialized windows pointer handler");
}
else if (UnityTouchHandler.IsSupported()) {
ptrHandler = new UnityTouchHandler(GetNewId, Callback);
Logger.Log("main", 1, "Input", "Initialized multi-platform pointer handler");
}
}
else if (Application.platform == RuntimePlatform.Android) {
/*if (AndroidTouchHandler.IsSupported()) {
}
else*
if (UnityTouchHandler.IsSupported()) {
ptrHandler = new UnityTouchHandler(GetNewId, Callback);
Logger.Log("main", 1, "Input", "Initialized multi-platform pointer handler");
}
}
else {
/*if (UnityPointerHandler.IsSupported()) {
enableUnityTouch();
}*
}
}
catch (Exception ex) {
Logger.Log("main", 4, "Input", "An error occured while initializing pointer handler:\n{0}", ex);
}
if (ptrHandler == null) Logger.Log("main", 3, "Input", "Pointer input is not supported on this device");
}
public void Activate() {
if (ptrHandler != null) ptrHandler.Activate();
}
public void Deactivate() {
if (ptrHandler != null) ptrHandler.Deactivate();
ptr.Clear(); ptrEv.Clear();
}
public void Dispose() {
ptrHandler.Dispose();
}
public void EnumeratePtrEvents(Action<PointerInfo> callback) {
lock (ptrEv) {
while (ptrEv.Count > 0) {
var raw = ptrEv.Dequeue();
raw.OffsetTime(originTime);
callback(raw);
}
}
}
public void EnumeratePointers(Action<PointerInfo> callback) {
lock (ptrEv) {
var upid = new List<int>();
var rmid = new List<int>();
foreach (var p in ptr) {
var raw = p.Value;
if (raw.Phase == PointerPhase.Stationary)
raw.EventTime = raw.ProcessTime = Time;
else raw.OffsetTime(originTime);
callback(raw);
if (raw.Phase == PointerPhase.Begin || raw.Phase == PointerPhase.Update)
upid.Add(p.Key);
if (raw.Phase == PointerPhase.End || raw.Phase == PointerPhase.Cancel)
rmid.Add(p.Key);
}
foreach (var i in upid) {
var p = ptr[i];
p.Phase = PointerPhase.Stationary;
ptr[i] = p;
}
foreach (var i in rmid) ptr.Remove(i);
// Logger.Log("main", 0, "Input", "Ptr count {0}", ptr.Count);
}
}
public double Time {
get {
if (ptrHandler != null) return ptrHandler.GetCurrentTimestamp() - originTime;
else return 0;
}
}
public void SyncTime(double t) {
if (ptrHandler != null) {
originTime = ptrHandler.GetCurrentTimestamp() - t;
Logger.Log("main", 0, "Input", "Sync time {0}", originTime);
}
}
}*/
public class InputManager {
static readonly List<Type> HandlerRegistries = new List<Type> {
typeof(WindowsPointerHandler),
typeof(UnityKeyHandler<UnityKeyboardReceiver>),
typeof(UnityKeyHandler<UnityMouseButtonReceiver>),
typeof(UnityMouseHandler),
typeof(UnityTouchHandler),
};
readonly List<InputHandler> _handlers = new List<InputHandler>();
readonly Dictionary<InputHandler, double> _timeOrigins = new Dictionary<InputHandler, double>();
readonly object _lock = new object();
readonly Dictionary<InputIdentifier, InputVector> _vectors = new Dictionary<InputIdentifier, InputVector>();
readonly List<InputEvent> _events = new List<InputEvent>();
public InputManager() {
var cb = new InputEventDelegate(Callback);
foreach (var t in HandlerRegistries) {
try {
if (!typeof(InputHandler).IsAssignableFrom(t)) continue;
var h = (InputHandler)ReflectionHelper.InvokeEmptyConstructor(t);
h.Callback = Callback;
_handlers.Add(h);
_timeOrigins.Add(h, 0);
Logger.Log("main", 1, "Input", "Initialized {0}", ReflectionHelper.GetSimpleName(t));
}
catch (TargetInvocationException ex) {
Logger.Log("main", 1, "Input", "Cannot initialize {0}: {1}", ReflectionHelper.GetSimpleName(t), ex.InnerException.Message);
}
}
}
public void Activate() {
lock (_lock) {
_events.Clear();
}
foreach (var h in _handlers) h.Activate();
}
public void SyncTime(double time) {
foreach (var h in _handlers) {
_timeOrigins[h] = time - h.GetCurrentTimestamp();
}
}
public void Deactivate() {
foreach (var h in _handlers) h.Deactivate();
}
void Callback(InputIdentifier id, InputVector vec) {
lock (_lock) {
double timeOrigin = _timeOrigins[id.Source.Handler];
vec.Time += timeOrigin;
InputVector vec0;
if (_vectors.TryGetValue(id, out vec0)) {
_events.Add(new InputEvent {
Id = id,
From = vec0,
To = vec,
});
if (vec.IsNull) _vectors.Remove(id);
else _vectors[id] = vec;
}
else {
_events.Add(new InputEvent {
Id = id,
From = new InputVector(vec.Time),
To = vec,
});
_vectors.Add(id, vec);
}
}
}
public void EnumerateEvents(Action<InputEvent> cb) {
lock (_lock) {
foreach (var ev in _events) cb(ev);
_events.Clear();
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: aaf7daeaf7afb3146b3eea2a07f88055
timeCreated: 1611035810
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,528 @@
namespace Cryville.Common.Unity.Input {
public enum WindowMessages : uint {
WM_NULL = 0x0000,
WM_CREATE = 0x0001,
WM_DESTROY = 0x0002,
WM_MOVE = 0x0003,
WM_SIZE = 0x0005,
WM_ACTIVATE = 0x0006,
/*
* WM_ACTIVATE state values
*/
WA_INACTIVE = 0,
WA_ACTIVE = 1,
WA_CLICKACTIVE = 2,
WM_SETFOCUS = 0x0007,
WM_KILLFOCUS = 0x0008,
WM_ENABLE = 0x000A,
WM_SETREDRAW = 0x000B,
WM_SETTEXT = 0x000C,
WM_GETTEXT = 0x000D,
WM_GETTEXTLENGTH = 0x000E,
WM_PAINT = 0x000F,
WM_CLOSE = 0x0010,
//#ifndef _WIN32_WCE
WM_QUERYENDSESSION = 0x0011,
WM_QUERYOPEN = 0x0013,
WM_ENDSESSION = 0x0016,
//#endif
WM_QUIT = 0x0012,
WM_ERASEBKGND = 0x0014,
WM_SYSCOLORCHANGE = 0x0015,
WM_SHOWWINDOW = 0x0018,
WM_WININICHANGE = 0x001A,
//#if(WINVER >= 0x0400)
WM_SETTINGCHANGE = WM_WININICHANGE,
//#endif /* WINVER >= 0x0400 */
WM_DEVMODECHANGE = 0x001B,
WM_ACTIVATEAPP = 0x001C,
WM_FONTCHANGE = 0x001D,
WM_TIMECHANGE = 0x001E,
WM_CANCELMODE = 0x001F,
WM_SETCURSOR = 0x0020,
WM_MOUSEACTIVATE = 0x0021,
WM_CHILDACTIVATE = 0x0022,
WM_QUEUESYNC = 0x0023,
WM_GETMINMAXINFO = 0x0024,
WM_PAINTICON = 0x0026,
WM_ICONERASEBKGND = 0x0027,
WM_NEXTDLGCTL = 0x0028,
WM_SPOOLERSTATUS = 0x002A,
WM_DRAWITEM = 0x002B,
WM_MEASUREITEM = 0x002C,
WM_DELETEITEM = 0x002D,
WM_VKEYTOITEM = 0x002E,
WM_CHARTOITEM = 0x002F,
WM_SETFONT = 0x0030,
WM_GETFONT = 0x0031,
WM_SETHOTKEY = 0x0032,
WM_GETHOTKEY = 0x0033,
WM_QUERYDRAGICON = 0x0037,
WM_COMPAREITEM = 0x0039,
//#if(WINVER >= 0x0500)
//#ifndef _WIN32_WCE
WM_GETOBJECT = 0x003D,
//#endif
//#endif /* WINVER >= 0x0500 */
WM_COMPACTING = 0x0041,
WM_COMMNOTIFY = 0x0044, /* no longer suported */
WM_WINDOWPOSCHANGING = 0x0046,
WM_WINDOWPOSCHANGED = 0x0047,
WM_POWER = 0x0048,
/*
* wParam for WM_POWER window message and DRV_POWER driver notification
*/
/*PWR_OK = 1,
PWR_FAIL = (-1),
PWR_SUSPENDREQUEST = 1,
PWR_SUSPENDRESUME = 2,
PWR_CRITICALRESUME = 3,*/
WM_COPYDATA = 0x004A,
WM_CANCELJOURNAL = 0x004B,
//#if(WINVER >= 0x0400)
WM_NOTIFY = 0x004E,
WM_INPUTLANGCHANGEREQUEST = 0x0050,
WM_INPUTLANGCHANGE = 0x0051,
WM_TCARD = 0x0052,
WM_HELP = 0x0053,
WM_USERCHANGED = 0x0054,
WM_NOTIFYFORMAT = 0x0055,
NFR_ANSI = 1,
NFR_UNICODE = 2,
NF_QUERY = 3,
NF_REQUERY = 4,
WM_CONTEXTMENU = 0x007B,
WM_STYLECHANGING = 0x007C,
WM_STYLECHANGED = 0x007D,
WM_DISPLAYCHANGE = 0x007E,
WM_GETICON = 0x007F,
WM_SETICON = 0x0080,
//#endif /* WINVER >= 0x0400 */
WM_NCCREATE = 0x0081,
WM_NCDESTROY = 0x0082,
WM_NCCALCSIZE = 0x0083,
WM_NCHITTEST = 0x0084,
WM_NCPAINT = 0x0085,
WM_NCACTIVATE = 0x0086,
WM_GETDLGCODE = 0x0087,
//#ifndef _WIN32_WCE
WM_SYNCPAINT = 0x0088,
//#endif
WM_NCMOUSEMOVE = 0x00A0,
WM_NCLBUTTONDOWN = 0x00A1,
WM_NCLBUTTONUP = 0x00A2,
WM_NCLBUTTONDBLCLK = 0x00A3,
WM_NCRBUTTONDOWN = 0x00A4,
WM_NCRBUTTONUP = 0x00A5,
WM_NCRBUTTONDBLCLK = 0x00A6,
WM_NCMBUTTONDOWN = 0x00A7,
WM_NCMBUTTONUP = 0x00A8,
WM_NCMBUTTONDBLCLK = 0x00A9,
//#if(_WIN32_WINNT >= 0x0500)
WM_NCXBUTTONDOWN = 0x00AB,
WM_NCXBUTTONUP = 0x00AC,
WM_NCXBUTTONDBLCLK = 0x00AD,
//#endif /* _WIN32_WINNT >= 0x0500 */
//#if(_WIN32_WINNT >= 0x0501)
WM_INPUT_DEVICE_CHANGE = 0x00FE,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#if(_WIN32_WINNT >= 0x0501)
WM_INPUT = 0x00FF,
//#endif /* _WIN32_WINNT >= 0x0501 */
WM_KEYFIRST = 0x0100,
WM_KEYDOWN = 0x0100,
WM_KEYUP = 0x0101,
WM_CHAR = 0x0102,
WM_DEADCHAR = 0x0103,
WM_SYSKEYDOWN = 0x0104,
WM_SYSKEYUP = 0x0105,
WM_SYSCHAR = 0x0106,
WM_SYSDEADCHAR = 0x0107,
//#if(_WIN32_WINNT >= 0x0501)
WM_UNICHAR = 0x0109,
WM_KEYLAST = 0x0109,
UNICODE_NOCHAR = 0xFFFF,
//#else
WM_KEYLAST__WIN2000 = 0x0108,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#if(WINVER >= 0x0400)
WM_IME_STARTCOMPOSITION = 0x010D,
WM_IME_ENDCOMPOSITION = 0x010E,
WM_IME_COMPOSITION = 0x010F,
WM_IME_KEYLAST = 0x010F,
//#endif /* WINVER >= 0x0400 */
WM_INITDIALOG = 0x0110,
WM_COMMAND = 0x0111,
WM_SYSCOMMAND = 0x0112,
WM_TIMER = 0x0113,
WM_HSCROLL = 0x0114,
WM_VSCROLL = 0x0115,
WM_INITMENU = 0x0116,
WM_INITMENUPOPUP = 0x0117,
//#if(WINVER >= 0x0601)
WM_GESTURE = 0x0119,
WM_GESTURENOTIFY = 0x011A,
//#endif /* WINVER >= 0x0601 */
WM_MENUSELECT = 0x011F,
WM_MENUCHAR = 0x0120,
WM_ENTERIDLE = 0x0121,
//#if(WINVER >= 0x0500)
//#ifndef _WIN32_WCE
WM_MENURBUTTONUP = 0x0122,
WM_MENUDRAG = 0x0123,
WM_MENUGETOBJECT = 0x0124,
WM_UNINITMENUPOPUP = 0x0125,
WM_MENUCOMMAND = 0x0126,
//#ifndef _WIN32_WCE
//#if(_WIN32_WINNT >= 0x0500)
WM_CHANGEUISTATE = 0x0127,
WM_UPDATEUISTATE = 0x0128,
WM_QUERYUISTATE = 0x0129,
/*
* LOWORD(wParam) values in WM_*UISTATE*
*/
UIS_SET = 1,
UIS_CLEAR = 2,
UIS_INITIALIZE = 3,
/*
* HIWORD(wParam) values in WM_*UISTATE*
*/
UISF_HIDEFOCUS = 0x1,
UISF_HIDEACCEL = 0x2,
//#if(_WIN32_WINNT >= 0x0501)
UISF_ACTIVE = 0x4,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#endif /* _WIN32_WINNT >= 0x0500 */
//#endif
//#endif
//#endif /* WINVER >= 0x0500 */
WM_CTLCOLORMSGBOX = 0x0132,
WM_CTLCOLOREDIT = 0x0133,
WM_CTLCOLORLISTBOX = 0x0134,
WM_CTLCOLORBTN = 0x0135,
WM_CTLCOLORDLG = 0x0136,
WM_CTLCOLORSCROLLBAR = 0x0137,
WM_CTLCOLORSTATIC = 0x0138,
MN_GETHMENU = 0x01E1,
WM_MOUSEFIRST = 0x0200,
WM_MOUSEMOVE = 0x0200,
WM_LBUTTONDOWN = 0x0201,
WM_LBUTTONUP = 0x0202,
WM_LBUTTONDBLCLK = 0x0203,
WM_RBUTTONDOWN = 0x0204,
WM_RBUTTONUP = 0x0205,
WM_RBUTTONDBLCLK = 0x0206,
WM_MBUTTONDOWN = 0x0207,
WM_MBUTTONUP = 0x0208,
WM_MBUTTONDBLCLK = 0x0209,
//#if (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
WM_MOUSEWHEEL = 0x020A,
//#endif
//#if (_WIN32_WINNT >= 0x0500)
WM_XBUTTONDOWN = 0x020B,
WM_XBUTTONUP = 0x020C,
WM_XBUTTONDBLCLK = 0x020D,
//#endif
//#if (_WIN32_WINNT >= 0x0600)
WM_MOUSEHWHEEL = 0x020E,
//#endif
//#if (_WIN32_WINNT >= 0x0600)
WM_MOUSELAST = 0x020E,
//#elif (_WIN32_WINNT >= 0x0500)
WM_MOUSELAST__WIN2000 = 0x020D,
//#elif (_WIN32_WINNT >= 0x0400) || (_WIN32_WINDOWS > 0x0400)
WM_MOUSELAST__WIN4 = 0x020A,
//#else
WM_MOUSELAST__WIN3 = 0x0209,
//#endif /* (_WIN32_WINNT >= 0x0600) */
//#if(_WIN32_WINNT >= 0x0400)
/* Value for rolling one detent */
WHEEL_DELTA = 120,
//GET_WHEEL_DELTA_WPARAM(wParam) ((short)HIWORD(wParam))
/* Setting to scroll one page for SPI_GET/SETWHEELSCROLLLINES */
WHEEL_PAGESCROLL = (uint.MaxValue),
//#endif /* _WIN32_WINNT >= 0x0400 */
//#if(_WIN32_WINNT >= 0x0500)
//GET_KEYSTATE_WPARAM(wParam) (LOWORD(wParam))
//GET_NCHITTEST_WPARAM(wParam) ((short)LOWORD(wParam))
//GET_XBUTTON_WPARAM(wParam) (HIWORD(wParam))
/* XButton values are WORD flags */
XBUTTON1 = 0x0001,
XBUTTON2 = 0x0002,
/* Were there to be an XBUTTON3, its value would be 0x0004 */
//#endif /* _WIN32_WINNT >= 0x0500 */
WM_PARENTNOTIFY = 0x0210,
WM_ENTERMENULOOP = 0x0211,
WM_EXITMENULOOP = 0x0212,
//#if(WINVER >= 0x0400)
WM_NEXTMENU = 0x0213,
WM_SIZING = 0x0214,
WM_CAPTURECHANGED = 0x0215,
WM_MOVING = 0x0216,
WM_POWERBROADCAST = 0x0218,
//#endif /* WINVER >= 0x0400 */
//#if(WINVER >= 0x0400)
WM_DEVICECHANGE = 0x0219,
//#endif /* WINVER >= 0x0400 */
WM_MDICREATE = 0x0220,
WM_MDIDESTROY = 0x0221,
WM_MDIACTIVATE = 0x0222,
WM_MDIRESTORE = 0x0223,
WM_MDINEXT = 0x0224,
WM_MDIMAXIMIZE = 0x0225,
WM_MDITILE = 0x0226,
WM_MDICASCADE = 0x0227,
WM_MDIICONARRANGE = 0x0228,
WM_MDIGETACTIVE = 0x0229,
WM_MDISETMENU = 0x0230,
WM_ENTERSIZEMOVE = 0x0231,
WM_EXITSIZEMOVE = 0x0232,
WM_DROPFILES = 0x0233,
WM_MDIREFRESHMENU = 0x0234,
//#if(WINVER >= 0x0602)
WM_POINTERDEVICECHANGE = 0x238,
WM_POINTERDEVICEINRANGE = 0x239,
WM_POINTERDEVICEOUTOFRANGE = 0x23A,
//#endif /* WINVER >= 0x0602 */
//#if(WINVER >= 0x0601)
WM_TOUCH = 0x0240,
//#endif /* WINVER >= 0x0601 */
//#if(WINVER >= 0x0602)
WM_NCPOINTERUPDATE = 0x0241,
WM_NCPOINTERDOWN = 0x0242,
WM_NCPOINTERUP = 0x0243,
WM_POINTERUPDATE = 0x0245,
WM_POINTERDOWN = 0x0246,
WM_POINTERUP = 0x0247,
WM_POINTERENTER = 0x0249,
WM_POINTERLEAVE = 0x024A,
WM_POINTERACTIVATE = 0x024B,
WM_POINTERCAPTURECHANGED = 0x024C,
WM_TOUCHHITTESTING = 0x024D,
WM_POINTERWHEEL = 0x024E,
WM_POINTERHWHEEL = 0x024F,
DM_POINTERHITTEST = 0x0250,
WM_POINTERROUTEDTO = 0x0251,
WM_POINTERROUTEDAWAY = 0x0252,
WM_POINTERROUTEDRELEASED = 0x0253,
//#endif /* WINVER >= 0x0602 */
//#if(WINVER >= 0x0400)
WM_IME_SETCONTEXT = 0x0281,
WM_IME_NOTIFY = 0x0282,
WM_IME_CONTROL = 0x0283,
WM_IME_COMPOSITIONFULL = 0x0284,
WM_IME_SELECT = 0x0285,
WM_IME_CHAR = 0x0286,
//#endif /* WINVER >= 0x0400 */
//#if(WINVER >= 0x0500)
WM_IME_REQUEST = 0x0288,
//#endif /* WINVER >= 0x0500 */
//#if(WINVER >= 0x0400)
WM_IME_KEYDOWN = 0x0290,
WM_IME_KEYUP = 0x0291,
//#endif /* WINVER >= 0x0400 */
//#if((_WIN32_WINNT >= 0x0400) || (WINVER >= 0x0500))
WM_MOUSEHOVER = 0x02A1,
WM_MOUSELEAVE = 0x02A3,
//#endif
//#if(WINVER >= 0x0500)
WM_NCMOUSEHOVER = 0x02A0,
WM_NCMOUSELEAVE = 0x02A2,
//#endif /* WINVER >= 0x0500 */
//#if(_WIN32_WINNT >= 0x0501)
WM_WTSSESSION_CHANGE = 0x02B1,
WM_TABLET_FIRST = 0x02c0,
WM_TABLET_LAST = 0x02df,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#if(WINVER >= 0x0601)
WM_DPICHANGED = 0x02E0,
//#endif /* WINVER >= 0x0601 */
//#if(WINVER >= 0x0605)
WM_DPICHANGED_BEFOREPARENT = 0x02E2,
WM_DPICHANGED_AFTERPARENT = 0x02E3,
WM_GETDPISCALEDSIZE = 0x02E4,
//#endif /* WINVER >= 0x0605 */
WM_CUT = 0x0300,
WM_COPY = 0x0301,
WM_PASTE = 0x0302,
WM_CLEAR = 0x0303,
WM_UNDO = 0x0304,
WM_RENDERFORMAT = 0x0305,
WM_RENDERALLFORMATS = 0x0306,
WM_DESTROYCLIPBOARD = 0x0307,
WM_DRAWCLIPBOARD = 0x0308,
WM_PAINTCLIPBOARD = 0x0309,
WM_VSCROLLCLIPBOARD = 0x030A,
WM_SIZECLIPBOARD = 0x030B,
WM_ASKCBFORMATNAME = 0x030C,
WM_CHANGECBCHAIN = 0x030D,
WM_HSCROLLCLIPBOARD = 0x030E,
WM_QUERYNEWPALETTE = 0x030F,
WM_PALETTEISCHANGING = 0x0310,
WM_PALETTECHANGED = 0x0311,
WM_HOTKEY = 0x0312,
//#if(WINVER >= 0x0400)
WM_PRINT = 0x0317,
WM_PRINTCLIENT = 0x0318,
//#endif /* WINVER >= 0x0400 */
//#if(_WIN32_WINNT >= 0x0500)
WM_APPCOMMAND = 0x0319,
//#endif /* _WIN32_WINNT >= 0x0500 */
//#if(_WIN32_WINNT >= 0x0501)
WM_THEMECHANGED = 0x031A,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#if(_WIN32_WINNT >= 0x0501)
WM_CLIPBOARDUPDATE = 0x031D,
//#endif /* _WIN32_WINNT >= 0x0501 */
//#if(_WIN32_WINNT >= 0x0600)
WM_DWMCOMPOSITIONCHANGED = 0x031E,
WM_DWMNCRENDERINGCHANGED = 0x031F,
WM_DWMCOLORIZATIONCOLORCHANGED= 0x0320,
WM_DWMWINDOWMAXIMIZEDCHANGE = 0x0321,
//#endif /* _WIN32_WINNT >= 0x0600 */
//#if(_WIN32_WINNT >= 0x0601)
WM_DWMSENDICONICTHUMBNAIL = 0x0323,
WM_DWMSENDICONICLIVEPREVIEWBITMAP = 0x0326,
//#endif /* _WIN32_WINNT >= 0x0601 */
//#if(WINVER >= 0x0600)
WM_GETTITLEBARINFOEX = 0x033F,
//#endif /* WINVER >= 0x0600 */
//#if(WINVER >= 0x0400)
//#endif /* WINVER >= 0x0400 */
//#if(WINVER >= 0x0400)
WM_HANDHELDFIRST = 0x0358,
WM_HANDHELDLAST = 0x035F,
WM_AFXFIRST = 0x0360,
WM_AFXLAST = 0x037F,
//#endif /* WINVER >= 0x0400 */
WM_PENWINFIRST = 0x0380,
WM_PENWINLAST = 0x038F,
//#if(WINVER >= 0x0400)
WM_APP = 0x8000,
//#endif /* WINVER >= 0x0400 */
/*
* NOTE: All Message Numbers below 0x0400 are RESERVED.
*
* Private Window Messages Start Here:
*/
WM_USER = 0x0400,
//#if(WINVER >= 0x0400)
/* wParam for WM_SIZING message */
WMSZ_LEFT = 1,
WMSZ_RIGHT = 2,
WMSZ_TOP = 3,
WMSZ_TOPLEFT = 4,
WMSZ_TOPRIGHT = 5,
WMSZ_BOTTOM = 6,
WMSZ_BOTTOMLEFT = 7,
WMSZ_BOTTOMRIGHT = 8,
//#endif /* WINVER >= 0x0400 */
//#ifndef NONCMESSAGES
/*
* WM_NCHITTEST and MOUSEHOOKSTRUCT Mouse Position Codes
*/
/*HTERROR = (-2),
HTTRANSPARENT = (-1),
HTNOWHERE = 0,
HTCLIENT = 1,
HTCAPTION = 2,
HTSYSMENU = 3,
HTGROWBOX = 4,
HTSIZE = HTGROWBOX,
HTMENU = 5,
HTHSCROLL = 6,
HTVSCROLL = 7,
HTMINBUTTON = 8,
HTMAXBUTTON = 9,
HTLEFT = 10,
HTRIGHT = 11,
HTTOP = 12,
HTTOPLEFT = 13,
HTTOPRIGHT = 14,
HTBOTTOM = 15,
HTBOTTOMLEFT = 16,
HTBOTTOMRIGHT = 17,
HTBORDER = 18,
HTREDUCE = HTMINBUTTON,
HTZOOM = HTMAXBUTTON,
HTSIZEFIRST = HTLEFT,
HTSIZELAST = HTBOTTOMRIGHT,
//#if(WINVER >= 0x0400)
HTOBJECT = 19,
HTCLOSE = 20,
HTHELP = 21,
//#endif /* WINVER >= 0x0400 */
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 466300df0840ba54d95240e3a800a642
timeCreated: 1611373988
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,253 @@
using UnityEngine;
using System;
using System.Runtime.InteropServices;
using System.Text;
namespace Cryville.Common.Unity.Input {
static class NativeMethods {
[StructLayout(LayoutKind.Sequential)]
public struct MONITORINFO {
public int cbSize;
public RECT rcMonitor;
public RECT rcWork;
public uint dwFlags;
}
[StructLayout(LayoutKind.Sequential)]
public struct RECT {
public int Left, Top, Right, Bottom;
public RECT(int left, int top, int right, int bottom) {
Left = left;
Top = top;
Right = right;
Bottom = bottom;
}
public int X {
get { return Left; }
set {
Right -= (Left - value);
Left = value;
}
}
public int Y {
get { return Top; }
set {
Bottom -= (Top - value);
Top = value;
}
}
public int Height {
get { return Bottom - Top; }
set { Bottom = value + Top; }
}
public int Width {
get { return Right - Left; }
set { Right = value + Left; }
}
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetTouchInputInfo(IntPtr hTouchInput, int cInputs, [Out] TOUCHINPUT[] pInputs, int cbSize);
[StructLayout(LayoutKind.Sequential)]
public struct TOUCHINPUT {
public int x;
public int y;
public IntPtr hSource;
public int dwID;
public TOUCHINPUT_Flags dwFlags;
public TOUCHINPUT_Mask dwMask;
public int dwTime;
public IntPtr dwExtraInfo;
public int cxContact;
public int cyContact;
}
[Flags]
public enum TOUCHINPUT_Flags : int {
TOUCHEVENTF_MOVE = 0x0001,
TOUCHEVENTF_DOWN = 0x0002,
TOUCHEVENTF_UP = 0x0004,
TOUCHEVENTF_INRANGE = 0x0008,
TOUCHEVENTF_PRIMARY = 0x0010,
TOUCHEVENTF_NOCOALESCE = 0x0020,
TOUCHEVENTF_PEN = 0x0040,
TOUCHEVENTF_PALM = 0x0080,
}
[Flags]
public enum TOUCHINPUT_Mask : int {
TOUCHINPUTMASKF_CONTACTAREA = 0x0004,
TOUCHINPUTMASKF_EXTRAINFO = 0x0002,
TOUCHINPUTMASKF_TIMEFROMSYSTEM = 0x0001,
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPointerInfo(int pointerID, ref POINTER_INFO pPointerInfo);
[StructLayout(LayoutKind.Sequential, Pack = 1)]
public struct POINTER_INFO {
public POINTER_INPUT_TYPE pointerType;
public UInt32 pointerId;
public UInt32 frameId;
public POINTER_FLAGS pointerFlags;
public IntPtr sourceDevice;
public IntPtr hwndTarget;
public POINT ptPixelLocation;
public POINT ptHimetricLocation;
public POINT ptPixelLocationRaw;
public POINT ptHimetricLocationRaw;
public UInt32 dwTime;
public UInt32 historyCount;
public Int32 inputData;
public UInt32 dwKeyStates;
public UInt64 PerformanceCount;
public POINTER_BUTTON_CHANGE_TYPE ButtonChangeType;
}
public enum POINTER_INPUT_TYPE {
PT_POINTER = 0x00000001,
PT_TOUCH = 0x00000002,
PT_PEN = 0x00000003,
PT_MOUSE = 0x00000004,
PT_TOUCHPAD = 0x00000005,
}
[Flags]
public enum POINTER_FLAGS {
POINTER_FLAG_NONE = 0x00000000,
POINTER_FLAG_NEW = 0x00000001,
POINTER_FLAG_INRANGE = 0x00000002,
POINTER_FLAG_INCONTACT = 0x00000004,
POINTER_FLAG_FIRSTBUTTON = 0x00000010,
POINTER_FLAG_SECONDBUTTON = 0x00000020,
POINTER_FLAG_THIRDBUTTON = 0x00000040,
POINTER_FLAG_FOURTHBUTTON = 0x00000080,
POINTER_FLAG_FIFTHBUTTON = 0x00000100,
POINTER_FLAG_PRIMARY = 0x00002000,
POINTER_FLAG_CONFIDENCE = 0x00004000,
POINTER_FLAG_CANCELED = 0x00008000,
POINTER_FLAG_DOWN = 0x00010000,
POINTER_FLAG_UPDATE = 0x00020000,
POINTER_FLAG_UP = 0x00040000,
POINTER_FLAG_WHEEL = 0x00080000,
POINTER_FLAG_HWHEEL = 0x00100000,
POINTER_FLAG_CAPTURECHANGED = 0x00200000,
POINTER_FLAG_HASTRANSFORM = 0x00400000,
}
public enum POINTER_BUTTON_CHANGE_TYPE {
POINTER_CHANGE_NONE,
POINTER_CHANGE_FIRSTBUTTON_DOWN,
POINTER_CHANGE_FIRSTBUTTON_UP,
POINTER_CHANGE_SECONDBUTTON_DOWN,
POINTER_CHANGE_SECONDBUTTON_UP,
POINTER_CHANGE_THIRDBUTTON_DOWN,
POINTER_CHANGE_THIRDBUTTON_UP,
POINTER_CHANGE_FOURTHBUTTON_DOWN,
POINTER_CHANGE_FOURTHBUTTON_UP,
POINTER_CHANGE_FIFTHBUTTON_DOWN,
POINTER_CHANGE_FIFTHBUTTON_UP,
}
[StructLayout(LayoutKind.Sequential)]
public struct POINT {
public int X;
public int Y;
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetPointerTouchInfo(int pointerId, ref POINTER_TOUCH_INFO touchInfo);
[StructLayout(LayoutKind.Sequential)]
public struct POINTER_TOUCH_INFO {
public POINTER_INFO pointerInfo;
public TOUCH_FLAGS touchFlags;
public TOUCH_MASK touchMask;
public RECT rcContact;
public RECT rcContactRaw;
public uint orientation;
public uint pressure;
}
[Flags]
public enum TOUCH_FLAGS {
TOUCH_FLAG_NONE = 0x00000000,
}
[Flags]
public enum TOUCH_MASK {
TOUCH_MASK_NONE = 0x00000000,
TOUCH_MASK_CONTACTAREA = 0x00000001,
TOUCH_MASK_ORIENTATION = 0x00000002,
TOUCH_MASK_PRESSURE = 0x00000004,
}
[DllImport("user32.dll")]
public static extern IntPtr EnableMouseInPointer(bool value);
[DllImport("kernel32.dll")]
public static extern uint GetCurrentThreadId();
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern int GetClassName(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
public delegate bool EnumWindowsProc(IntPtr hWnd,IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool EnumThreadWindows(uint dwThreadId, EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll")]
public static extern IntPtr MonitorFromWindow(IntPtr hwnd, uint dwFlags);
[DllImport("user32.dll")]
public static extern bool GetMonitorInfo(IntPtr hMonitor, ref MONITORINFO lpmi);
[DllImport("user32.dll", EntryPoint = "SetWindowLong")]
public static extern int SetWindowLong32(IntPtr hWnd, int nIndex, int dwNewLong);
[DllImport("user32.dll", EntryPoint = "SetWindowLongPtr")]
public static extern IntPtr SetWindowLongPtr64(IntPtr hWnd, int nIndex, IntPtr dwNewLong);
[DllImport("user32.dll")]
public static extern IntPtr CallWindowProc(IntPtr lpPrevWndFunc, IntPtr hWnd, uint msg, IntPtr wParam,
IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool RegisterTouchWindow(IntPtr hWnd, TOUCH_WINDOW_FLAGS ulFlags);
[Flags]
public enum TOUCH_WINDOW_FLAGS {
TWF_FINETOUCH = 1,
TWF_WANTPALM = 2,
}
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool UnregisterTouchWindow(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern void CloseTouchInputHandle(IntPtr lParam);
[DllImport("user32.dll")]
public static extern bool ScreenToClient(IntPtr hWnd, ref POINT lpPoint);
[DllImport("Kernel32.dll", CharSet = CharSet.Unicode)]
public static extern ushort GlobalAddAtom(string lpString);
[DllImport("Kernel32.dll")]
public static extern ushort GlobalDeleteAtom(ushort nAtom);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int SetProp(IntPtr hWnd, string lpString, int hData);
[DllImport("user32.dll", CharSet = CharSet.Unicode)]
public static extern int RemoveProp(IntPtr hWnd, string lpString);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
[DllImport("kernel32.dll")]
public static extern uint QueryPerformanceFrequency(out Int64 lpFrequency);
[DllImport("kernel32.dll")]
public static extern uint QueryPerformanceCounter(out Int64 lpPerformanceCount);
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: a6ff72ea2b7f71345aa19940faf026e8
timeCreated: 1622589747
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,57 @@
using System;
using UnityEngine;
namespace Cryville.Common.Unity.Input {
#if false
[Obsolete]
public abstract class PointerHandler {
protected Func<int> newIdCallback;
protected Action<int, PointerInfo> callback;
public PointerHandler(Func<int> newIdCallback, Action<int, PointerInfo> callback) {
this.newIdCallback = newIdCallback;
this.callback = callback;
}
public abstract void Activate();
public abstract void Deactivate();
public abstract void Dispose();
public abstract double GetCurrentTimestamp();
}
[Obsolete]
public struct PointerInfo {
public int Id;
public double EventTime;
public double ProcessTime;
public PointerPhase Phase;
public PointerType Type;
public Vector2 Position;
public Vector2? ContactSize;
public uint? Orientation;
public uint? Pressure;
public double Time {
// get { return EventTime == 0 ? ProcessTime : EventTime; }
get { return ProcessTime; }
}
public void OffsetTime(double originTime) {
if (EventTime != 0) EventTime -= originTime;
if (ProcessTime != 0) ProcessTime -= originTime;
}
}
[Obsolete]
public enum PointerPhase {
Begin = 0,
Update = 2, Stationary = 3,
End = 4, Cancel = 5
}
[Obsolete]
public enum PointerType {
Unknown, Mouse, Touch, Pen, TouchPad
}
#endif
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 6f0a4eae2ebff4d4d93e2f9fe31c1383
timeCreated: 1611272693
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,112 @@
using System;
using System.Collections.Generic;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Common.Unity.Input {
public class UnityKeyHandler<T> : InputHandler where T : UnityKeyReceiver<T> {
GameObject receiver;
T recvcomp;
public UnityKeyHandler() { }
public override void Activate() {
receiver = new GameObject("__keyrecv__");
recvcomp = receiver.AddComponent<T>();
recvcomp.SetCallback(OnInput);
}
public override void Deactivate() {
if (receiver) GameObject.Destroy(receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable(int type) {
return false;
}
public override byte GetDimension(int type) {
return 0;
}
public override string GetTypeName(int type) {
return recvcomp.GetKeyName(type);
}
public override double GetCurrentTimestamp() {
return Time.timeAsDouble;
}
}
public abstract class UnityKeyReceiver<T> : MonoBehaviour where T : UnityKeyReceiver<T> {
protected Action<int, int, InputVector> Callback;
protected readonly List<int> Keys = new List<int>();
public void SetCallback(Action<int, int, InputVector> h) {
Callback = h;
}
public abstract string GetKeyName(int type);
void Update() {
double time = Time.timeAsDouble;
foreach (var k in Keys) {
Callback(k, 0, new InputVector(time, Vector3.zero));
}
}
}
public class UnityKeyboardReceiver : UnityKeyReceiver<UnityKeyboardReceiver> {
public override string GetKeyName(int type) {
return Enum.GetName(typeof(KeyCode), type);
}
void OnGUI() {
var e = Event.current;
if (e.keyCode == KeyCode.None) return;
double time = Time.timeAsDouble;
var key = (int)e.keyCode;
switch (e.type) {
case EventType.KeyDown:
if (!Keys.Contains(key)) {
Callback(key, 0, new InputVector(time, Vector3.zero));
Keys.Add(key);
}
break;
case EventType.KeyUp:
Keys.Remove(key);
Callback(key, 0, new InputVector(time));
break;
}
}
}
public class UnityMouseButtonReceiver : UnityKeyReceiver<UnityMouseButtonReceiver> {
public override string GetKeyName(int type) {
switch (type) {
case 0: return "Left";
case 1: return "Right";
case 2: return "Middle";
default: return string.Format("Button {0}", type);
}
}
void OnGUI() {
var e = Event.current;
double time = Time.timeAsDouble;
var key = e.button;
switch (e.type) {
case EventType.MouseDown:
if (!Keys.Contains(key)) {
Callback(key, 0, new InputVector(time, Vector3.zero));
Keys.Add(key);
}
break;
case EventType.MouseUp:
Keys.Remove(key);
Callback(key, 0, new InputVector(time));
break;
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 8aa36fa1966064f489cf5c318f576d3f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,64 @@
using System;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Common.Unity.Input {
public class UnityMouseHandler : InputHandler {
GameObject receiver;
public UnityMouseHandler() {
if (!unity::Input.mousePresent) {
throw new NotSupportedException("Unity mouse is not supported on this device");
}
}
public override void Activate() {
receiver = new GameObject("__mouserecv__");
receiver.AddComponent<UnityMouseReceiver>().SetHandler(this);
}
public override void Deactivate() {
if (receiver) GameObject.Destroy(receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return false;
}
public override byte GetDimension(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return 2;
}
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Unity Mouse";
default: throw new ArgumentOutOfRangeException(nameof(type));
}
}
public override double GetCurrentTimestamp() {
return Time.timeAsDouble;
}
public class UnityMouseReceiver : MonoBehaviour {
UnityMouseHandler handler;
public void SetHandler(UnityMouseHandler h) {
handler = h;
}
void Update() {
double time = Time.timeAsDouble;
Vector2 pos = unity::Input.mousePosition;
pos.y = Screen.height - pos.y;
handler.OnInput(0, 0, new InputVector(time, pos));
}
}
}
}

View File

@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 946e4d6c783bbd645a484b5612476526
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,74 @@
using System;
using UnityEngine;
using unity = UnityEngine;
namespace Cryville.Common.Unity.Input {
public class UnityTouchHandler : InputHandler {
GameObject receiver;
public UnityTouchHandler() {
if (!unity::Input.touchSupported) {
throw new NotSupportedException("Unity touch is not supported on this device");
}
}
public override void Activate() {
receiver = new GameObject("__touchrecv__");
receiver.AddComponent<UnityPointerReceiver>().SetHandler(this);
}
public override void Deactivate() {
if (receiver) GameObject.Destroy(receiver);
}
public override void Dispose(bool disposing) {
if (disposing) {
Deactivate();
}
}
public override bool IsNullable(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return true;
}
public override byte GetDimension(int type) {
if (type != 0) throw new ArgumentOutOfRangeException(nameof(type));
return 2;
}
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Unity Touch";
default: throw new ArgumentOutOfRangeException(nameof(type));
}
}
public override double GetCurrentTimestamp() {
return Time.timeAsDouble;
}
public class UnityPointerReceiver : MonoBehaviour {
UnityTouchHandler handler;
public void SetHandler(UnityTouchHandler h) {
handler = h;
}
void Update() {
double time = Time.timeAsDouble;
for (int i = 0; i < unity::Input.touchCount; i++) {
var t = unity::Input.GetTouch(i);
Vector2 pos = t.position;
pos.y = Screen.height - pos.y;
var vec = new InputVector(time, pos);
if (t.phase == TouchPhase.Began || t.phase == TouchPhase.Stationary || t.phase == TouchPhase.Moved) {
handler.OnInput(0, t.fingerId, vec);
}
else if (t.phase == TouchPhase.Ended || t.phase == TouchPhase.Canceled) {
handler.OnInput(0, t.fingerId, vec);
handler.OnInput(0, t.fingerId, new InputVector(time));
}
}
}
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 93f60577ebaa5824dba5f322bbd1ad26
timeCreated: 1618910605
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,360 @@
/*
* @author Valentin Simonov / http://va.lent.in/
* @author Valentin Frolov
* @author Andrew David Griffiths
* @author Cryville:Lime
*/
using AOT;
using System;
using System.Runtime.InteropServices;
using System.Text;
using UnityEngine;
namespace Cryville.Common.Unity.Input {
public class WindowsPointerHandler : InputHandler {
static WindowsPointerHandler Instance;
const string PRESS_AND_HOLD_ATOM = "MicrosoftTabletPenServiceProperty";
delegate IntPtr WndProcDelegate(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);
public const int MONITOR_DEFAULTTONEAREST = 2;
readonly IntPtr hMainWindow;
IntPtr oldWndProcPtr;
IntPtr newWndProcPtr;
WndProcDelegate newWndProc;
ushort pressAndHoldAtomID;
readonly int touchInputSize;
readonly long freq;
static bool usePointerMessage;
public WindowsPointerHandler() {
if (Instance != null)
throw new InvalidOperationException("WindowsPointerHandler already created");
Instance = this;
usePointerMessage = true;
hMainWindow = GetUnityWindowHandle();
if (hMainWindow == IntPtr.Zero) {
throw new InvalidOperationException("Cannot find the game window");
}
NativeMethods.QueryPerformanceFrequency(out freq);
retry:
if (usePointerMessage) {
try {
NativeMethods.EnableMouseInPointer(true);
Logger.Log("main", 0, "Input", "Touch input frequency: {0}", freq);
}
catch (TypeLoadException) {
usePointerMessage = false;
Logger.Log("main", 2, "Input", "Advanced touch handling not supported");
goto retry;
}
}
else {
touchInputSize = Marshal.SizeOf(typeof(NativeMethods.TOUCHINPUT));
}
}
public override void Activate() {
RegisterWindowProc(WndProc);
if (!NativeMethods.RegisterTouchWindow(hMainWindow, NativeMethods.TOUCH_WINDOW_FLAGS.TWF_WANTPALM)) {
throw new InvalidOperationException("Failed to register touch window");
}
DisablePressAndHold();
}
public override void Deactivate() {
EnablePressAndHold();
if (!NativeMethods.UnregisterTouchWindow(hMainWindow)) {
throw new InvalidOperationException("Failed to unregister touch window");
}
UnregisterWindowProc();
}
void RegisterWindowProc(WndProcDelegate windowProc) {
newWndProc = windowProc;
newWndProcPtr = Marshal.GetFunctionPointerForDelegate(newWndProc);
oldWndProcPtr = SetWindowLongPtr(hMainWindow, -4, newWndProcPtr);
}
void UnregisterWindowProc() {
SetWindowLongPtr(hMainWindow, -4, oldWndProcPtr);
newWndProcPtr = IntPtr.Zero;
newWndProc = null;
}
public const int TABLET_DISABLE_PRESSANDHOLD = 0x00000001;
public const int TABLET_DISABLE_PENTAPFEEDBACK = 0x00000008;
public const int TABLET_DISABLE_PENBARRELFEEDBACK = 0x00000010;
public const int TABLET_DISABLE_FLICKS = 0x00010000;
protected void DisablePressAndHold() {
pressAndHoldAtomID = NativeMethods.GlobalAddAtom(PRESS_AND_HOLD_ATOM);
NativeMethods.SetProp(hMainWindow, PRESS_AND_HOLD_ATOM,
TABLET_DISABLE_PRESSANDHOLD | // disables press and hold (right-click) gesture
TABLET_DISABLE_PENTAPFEEDBACK | // disables UI feedback on pen up (waves)
TABLET_DISABLE_PENBARRELFEEDBACK | // disables UI feedback on pen button down (circle)
TABLET_DISABLE_FLICKS // disables pen flicks (back, forward, drag down, drag up);
);
}
protected void EnablePressAndHold() {
if (pressAndHoldAtomID != 0) {
NativeMethods.RemoveProp(hMainWindow, PRESS_AND_HOLD_ATOM);
NativeMethods.GlobalDeleteAtom(pressAndHoldAtomID);
}
}
const string UnityWindowClassName = "UnityWndClass";
static IntPtr _windowHandle;
public static IntPtr GetUnityWindowHandle() {
uint threadId = NativeMethods.GetCurrentThreadId();
NativeMethods.EnumThreadWindows(
threadId,
UnityWindowFilter,
IntPtr.Zero
);
return _windowHandle;
}
[MonoPInvokeCallback(typeof(NativeMethods.EnumWindowsProc))]
public static bool UnityWindowFilter(IntPtr hWnd, IntPtr lParam) {
var classText = new StringBuilder(UnityWindowClassName.Length + 1);
NativeMethods.GetClassName(hWnd, classText, classText.Capacity);
if (classText.ToString() == UnityWindowClassName) {
_windowHandle = hWnd;
return false;
}
return true;
}
public override bool IsNullable(int type) {
return true;
}
public override byte GetDimension(int type) {
return 2;
}
public override string GetTypeName(int type) {
switch (type) {
case 0: return "Windows Pointer Unknown";
case 1: return "Windows Pointer Pointer";
case 2: return "Windows Pointer Touch";
case 3: return "Windows Pointer Pen";
case 4: return "Windows Pointer Mouse";
case 5: return "Windows Pointer Touch Pad";
case 255: return "Windows Touch";
default: throw new ArgumentOutOfRangeException(nameof(type));
}
}
public override double GetCurrentTimestamp() {
long tick;
NativeMethods.QueryPerformanceCounter(out tick);
return (double)tick / freq;
}
public override void Dispose(bool disposing) {
Deactivate();
if (usePointerMessage)
NativeMethods.EnableMouseInPointer(false);
Instance = null;
}
[MonoPInvokeCallback(typeof(WndProcDelegate))]
static IntPtr WndProc(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam) {
try {
var m = (WindowMessages)msg;
/*if (m >= WindowMessages.WM_MOUSEFIRST && m <= WindowMessages.WM_MOUSELAST) {
if (!usePointerMessage)
Instance.handleMouseMsg(msg, wParam, lParam);
}
else*/
switch (m) {
case WindowMessages.WM_TOUCH:
if (usePointerMessage)
NativeMethods.CloseTouchInputHandle(lParam);
else
Instance.HandleTouchMsg(wParam, lParam);
break;
case WindowMessages.WM_POINTERDOWN:
case WindowMessages.WM_POINTERUP:
case WindowMessages.WM_POINTERUPDATE:
if (usePointerMessage)
Instance.HandlePointerMsg(msg, wParam, lParam);
break;
case WindowMessages.WM_CLOSE:
Instance.Dispose();
// Calling oldWndProcPtr here would crash the application
Application.Quit();
return IntPtr.Zero;
}
return NativeMethods.CallWindowProc(Instance.oldWndProcPtr, hWnd, msg, wParam, lParam);
}
catch (Exception ex) {
Logger.Log("main", 4, "Input", "An error occured while handling pointer:\n{0}", ex);
return IntPtr.Zero;
}
}
void HandlePointerMsg(uint msg, IntPtr wParam, IntPtr lParam) {
int id = LOWORD(wParam.ToInt32());
var rawpinfo = new NativeMethods.POINTER_INFO();
if (!NativeMethods.GetPointerInfo(id, ref rawpinfo))
throw new InvalidOperationException("Failed to get pointer info");
/*
Vector2? _cs = null;
uint? orientation = null;
uint? pressure = null;
if (pointerInfo.pointerType == NativeMethods.POINTER_INPUT_TYPE.PT_TOUCH) {
var pointerTouchInfo = new NativeMethods.POINTER_TOUCH_INFO();
NativeMethods.GetPointerTouchInfo(pointerId, ref pointerTouchInfo);
if (pointerTouchInfo.touchMask.HasFlag(NativeMethods.TOUCH_MASK.TOUCH_MASK_CONTACTAREA)) {
Vector2 cs;
cs.x = pointerTouchInfo.rcContact.Width;
cs.y = pointerTouchInfo.rcContact.Height;
_cs = cs;
}
if (pointerTouchInfo.touchMask.HasFlag(NativeMethods.TOUCH_MASK.TOUCH_MASK_ORIENTATION)) {
orientation = pointerTouchInfo.orientation;
}
if (pointerTouchInfo.touchMask.HasFlag(NativeMethods.TOUCH_MASK.TOUCH_MASK_PRESSURE)) {
pressure = pointerTouchInfo.pressure;
}
}*/
NativeMethods.POINT p = rawpinfo.ptPixelLocation;
NativeMethods.ScreenToClient(hMainWindow, ref p);
Vector2 _p = new Vector2(p.X, p.Y);
double time = (double)rawpinfo.PerformanceCount / freq;
int type;
switch (rawpinfo.pointerType) {
case NativeMethods.POINTER_INPUT_TYPE.PT_POINTER : type = 1; break;
case NativeMethods.POINTER_INPUT_TYPE.PT_TOUCH : type = 2; break;
case NativeMethods.POINTER_INPUT_TYPE.PT_PEN : type = 3; break;
case NativeMethods.POINTER_INPUT_TYPE.PT_MOUSE : type = 4; break;
case NativeMethods.POINTER_INPUT_TYPE.PT_TOUCHPAD: type = 5; break;
default: type = 0; break;
}
if (rawpinfo.pointerFlags.HasFlag(NativeMethods.POINTER_FLAGS.POINTER_FLAG_CANCELED)) {
OnInput(type, id, new InputVector(time));
return;
}
InputVector vec = new InputVector(time, _p);
switch ((WindowMessages)msg) {
case WindowMessages.WM_POINTERDOWN:
case WindowMessages.WM_POINTERUPDATE:
OnInput(type, id, vec);
break;
case WindowMessages.WM_POINTERUP:
OnInput(type, id, vec);
OnInput(type, id, new InputVector(time));
break;
}
}
/*void handleMouseMsg(uint msg, IntPtr wParam, IntPtr lParam) {
int id = mouseId;
double procTime = diag::Stopwatch.GetTimestamp()
/ (double)diag::Stopwatch.Frequency;
Vector2 _p;
_p.x = ((int)(short)LOWORD(lParam.ToInt32()));
_p.y = ((int)(short)HIWORD(lParam.ToInt32()));
PointerPhase phase;
switch ((WindowMessages)msg) {
case WindowMessages.WM_LBUTTONDOWN:
case WindowMessages.WM_LBUTTONDBLCLK:
phase = PointerPhase.Begin;
id = mouseId = newIdCallback();
break;
case WindowMessages.WM_MOUSEMOVE:
if (mouseId == 0)
return;
phase = PointerPhase.Update;
break;
case WindowMessages.WM_LBUTTONUP:
phase = PointerPhase.End;
mouseId = 0;
break;
case WindowMessages.WM_MOUSELEAVE:
phase = PointerPhase.Cancel;
mouseId = 0;
break;
default:
return;
}
callback(id, new PointerInfo() {
Id = id,
ProcessTime = procTime,
Phase = phase,
Type = PointerType.Mouse,
Position = _p,
});
}*/
void HandleTouchMsg(IntPtr wParam, IntPtr lParam) {
int inputCount = LOWORD(wParam.ToInt32());
NativeMethods.TOUCHINPUT[] inputs = new NativeMethods.TOUCHINPUT[inputCount];
if (!NativeMethods.GetTouchInputInfo(lParam, inputCount, inputs, touchInputSize))
throw new InvalidOperationException("Failed to get touch input info");
for (int i = 0; i < inputCount; i++) {
NativeMethods.TOUCHINPUT touch = inputs[i];
NativeMethods.POINT p = new NativeMethods.POINT {
X = touch.x / 100,
Y = touch.y / 100
};
NativeMethods.ScreenToClient(hMainWindow, ref p);
Vector2 _p = new Vector2(p.X, p.Y);
/*Vector2? _cs = null;
if (touch.dwMask.HasFlag(NativeMethods.TOUCHINPUT_Mask.TOUCHINPUTMASKF_CONTACTAREA)) {
Vector2 cs;
cs.x = touch.cxContact / 100;
cs.y = touch.cyContact / 100;
_cs = cs;
}*/
int id = touch.dwID;
double time = touch.dwTime / 1000.0;
InputVector vec = new InputVector(time, _p);
if (touch.dwFlags.HasFlag(NativeMethods.TOUCHINPUT_Flags.TOUCHEVENTF_MOVE) ||
touch.dwFlags.HasFlag(NativeMethods.TOUCHINPUT_Flags.TOUCHEVENTF_DOWN)) {
OnInput(255, id, vec);
}
else if (touch.dwFlags.HasFlag(NativeMethods.TOUCHINPUT_Flags.TOUCHEVENTF_UP)) {
OnInput(255, id, vec);
OnInput(255, id, new InputVector(time));
}
}
NativeMethods.CloseTouchInputHandle(lParam);
}
public static int LOWORD(int value) {
return value & 0xffff;
}
public static int HIWORD(int value) {
return (value & (0xffff << 16)) >> 16;
}
public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, IntPtr dwNewLong) {
if (IntPtr.Size == 8)
return NativeMethods.SetWindowLongPtr64(hWnd, nIndex, dwNewLong);
return new IntPtr(NativeMethods.SetWindowLong32(hWnd, nIndex, dwNewLong.ToInt32()));
}
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: d1b353deb73c51b409b15e54c54a6bb1
timeCreated: 1611282071
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,138 @@
using System;
using System.Collections.Generic;
using UnityEngine;
#if UNITY_5_4_OR_NEWER
using UnityEngine.Networking;
using UnityEngine.Rendering;
using static UnityEngine.Networking.UnityWebRequest;
#endif
namespace Cryville.Common.Unity {
public class NetworkTaskWorker {
bool suspended;
NetworkTask currentNetworkTask;
readonly Queue<NetworkTask> networkTasks = new Queue<NetworkTask>();
public int TaskCount { get { return networkTasks.Count; } }
public void SubmitNetworkTask(NetworkTask task) {
networkTasks.Enqueue(task);
}
public WorkerStatus TickBackgroundTasks() {
if (suspended) return WorkerStatus.Suspended;
if (currentNetworkTask != null) {
if (currentNetworkTask.Canceled) currentNetworkTask = null;
else if (currentNetworkTask.Done()) currentNetworkTask = null;
}
while (networkTasks.Count > 0 && currentNetworkTask == null) {
var task = networkTasks.Dequeue();
if (task.Canceled) continue;
currentNetworkTask = task;
currentNetworkTask.Start();
}
return currentNetworkTask == null ? WorkerStatus.Idle : WorkerStatus.Working;
}
public void SuspendBackgroundTasks() {
suspended = true;
if (currentNetworkTask != null) {
currentNetworkTask.Cancel();
currentNetworkTask = null;
}
}
public void ResumeBackgroundTasks() {
suspended = false;
}
}
public enum WorkerStatus {
Idle, Working, Suspended,
}
public abstract class NetworkTask {
protected NetworkTask(string uri) {
Uri = uri;
}
public string Uri { get; private set; }
public bool Canceled { get; private set; }
public virtual void Cancel() {
Canceled = true;
}
#if UNITY_5_4_OR_NEWER
protected UnityWebRequest www;
public virtual void Start() {
www = new UnityWebRequest(Uri);
www.SendWebRequest();
}
public virtual bool Done() {
if (!www.isDone) return false;
return true;
}
#else
protected WWW www;
public virtual WWW Start() {
return new WWW(Uri);
}
public abstract void Done();
#endif
}
public class LoadTextureTask : NetworkTask {
public LoadTextureTask(string uri, Action<bool, Texture2D> callback) : base(uri) {
Callback = callback;
}
public Action<bool, Texture2D> Callback { get; private set; }
#if UNITY_5_4_OR_NEWER
DownloadHandlerTexture handler;
public override void Start() {
handler = new DownloadHandlerTexture();
www = new UnityWebRequest(Uri, "GET", handler, null);
www.SendWebRequest();
}
public override bool Done() {
if (!handler.isDone) return false;
if (handler.texture != null) {
var buffer = handler.texture;
/*var result = new Texture2D(buffer.width, buffer.height, buffer.format, true);
if (SystemInfo.copyTextureSupport.HasFlag(CopyTextureSupport.Basic)) {
Graphics.CopyTexture(buffer, 0, 0, result, 0, 0);
}
else {
result.LoadImage(handler.data);
}
result.Apply(true, true);
Texture2D.Destroy(buffer);
Callback(true, result);*/
Callback(true, buffer);
}
else {
Callback(false, null);
}
www.Dispose();
handler.Dispose();
return true;
}
#else
public override void PostProcess(WWW www) {
bool succeeded = string.IsNullOrEmpty(www.error);
if (succeeded) {
var buffer = www.texture;
var result = new Texture2D(buffer.width, buffer.height, buffer.format, true);
result.SetPixels(buffer.GetPixels());
result.Apply(true, true);
Texture2D.Destroy(buffer);
Callback(true, result);
}
else Callback(false, null);
}
#endif
}
}

View File

@@ -0,0 +1,12 @@
fileFormatVersion: 2
guid: 1641131deb5de004a9a6d7ddb03d9dac
timeCreated: 1637837586
licenseType: Free
MonoImporter:
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

Some files were not shown because too many files have changed in this diff Show More