Browse Source

Merge pull request #669 from shangfengh/new

feat(SafeValue):  add some SafeValue
dev
shangfengh GitHub 2 years ago
parent
commit
ea69149ddb
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 548 additions and 137 deletions
  1. +2
    -2
      logic/GameClass/GameObj/Map/Chest.cs
  2. +12
    -2
      logic/GameClass/GameObj/ObjOfCharacter.cs
  3. +35
    -12
      logic/Preparation/Utility/SafeValue/Atomic.cs
  4. +382
    -113
      logic/Preparation/Utility/SafeValue/InTheRange.cs
  5. +117
    -8
      logic/Preparation/Utility/SafeValue/TimeBased.cs

+ 2
- 2
logic/GameClass/GameObj/Map/Chest.cs View File

@@ -18,7 +18,7 @@ namespace GameClass.GameObj
private readonly Gadget[] propInChest = new Gadget[GameData.maxNumOfPropInChest] { new NullProp(), new NullProp() };
public Gadget[] PropInChest => propInChest;

private TimeBasedProgressForInterrupting openProgress = new TimeBasedProgressForInterrupting();
public TimeBasedProgressForInterrupting OpenProgress { get => openProgress; }
private TimeBasedProgressOptimizedForInterrupting openProgress = new TimeBasedProgressOptimizedForInterrupting();
public TimeBasedProgressOptimizedForInterrupting OpenProgress { get => openProgress; }
}
}

+ 12
- 2
logic/GameClass/GameObj/ObjOfCharacter.cs View File

@@ -16,17 +16,27 @@ namespace GameClass.GameObj
{
get
{
lock (objOfCharacterReaderWriterLock)
objOfCharacterReaderWriterLock.EnterReadLock();
try
{
return parent;
}
finally
{
objOfCharacterReaderWriterLock.ExitReadLock();
}
}
set
{
lock (objOfCharacterReaderWriterLock)
objOfCharacterReaderWriterLock.EnterWriteLock();
try
{
parent = value;
}
finally
{
objOfCharacterReaderWriterLock.ExitWriteLock();
}
}
}
// LHR注:本来考虑在构造函数里设置parent属性,见THUAI4在游戏引擎中才设置该属性,作罢。——2021/9/24


+ 35
- 12
logic/Preparation/Utility/SafeValue/Atomic.cs View File

@@ -4,7 +4,11 @@ using System.Threading;
namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值
public class AtomicInt
public abstract class Atomic
{
}

public class AtomicInt : Atomic
{
private int v;
public AtomicInt(int x)
@@ -24,7 +28,7 @@ namespace Preparation.Utility
public int CompareExReturnOri(int newV, int compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicLong
public class AtomicLong : Atomic
{
private long v;
public AtomicLong(long x)
@@ -33,7 +37,7 @@ namespace Preparation.Utility
}
public override string ToString() => Interlocked.Read(ref v).ToString();
public long Get() => Interlocked.Read(ref v);
public static implicit operator long(AtomicLong aint) => Interlocked.Read(ref aint.v);
public static implicit operator long(AtomicLong along) => Interlocked.Read(ref along.v);
/// <returns>返回操作前的值</returns>
public long SetReturnOri(long value) => Interlocked.Exchange(ref v, value);
public long Add(long x) => Interlocked.Add(ref v, x);
@@ -44,24 +48,43 @@ namespace Preparation.Utility
public long CompareExReturnOri(long newV, long compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicBool
public class AtomicDouble : Atomic
{
private double v;
public AtomicDouble(double x)
{
v = x;
}
public override string ToString() => Interlocked.CompareExchange(ref v, -2.0, -2.0).ToString();
public double Get() => Interlocked.CompareExchange(ref v, -2.0, -2.0);
public static implicit operator double(AtomicDouble adouble) => Interlocked.CompareExchange(ref adouble.v, -2.0, -2.0);
/// <returns>返回操作前的值</returns>
public double SetReturnOri(double value) => Interlocked.Exchange(ref v, value);
/// <returns>返回操作前的值</returns>
public double CompareExReturnOri(double newV, double compareTo) => Interlocked.CompareExchange(ref v, newV, compareTo);
}

public class AtomicBool : Atomic
{
private int v;//v==0为false,v==1为true
private int v;//v&1==0为false,v&1==1为true
public AtomicBool(bool x)
{
v = x ? 1 : 0;
}
public override string ToString() => (Interlocked.CompareExchange(ref v, -2, -2) == 0) ? "false" : "true";
public bool Get() => (Interlocked.CompareExchange(ref v, -1, -1) != 0);
public static implicit operator bool(AtomicBool abool) => (Interlocked.CompareExchange(ref abool.v, -1, -1) != 0);
public override string ToString() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 0) ? "false" : "true";
public bool Get() => ((Interlocked.CompareExchange(ref v, -2, -2) & 1) == 1);
public static implicit operator bool(AtomicBool abool) => abool.Get();
/// <returns>返回操作前的值</returns>
public bool SetReturnOri(bool value) => (Interlocked.Exchange(ref v, value ? 1 : 0) != 0);
public bool SetReturnOri(bool value) => ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) == 1);
/// <returns>赋值前的值是否与将赋予的值不相同</returns>
public bool TrySet(bool value)
{
return (Interlocked.CompareExchange(ref v, value ? 1 : 0, value ? 0 : 1) ^ (value ? 1 : 0)) != 0;
return ((Interlocked.Exchange(ref v, value ? 1 : 0) & 1) != (value ? 1 : 0));
}
public bool And(bool x) => Interlocked.And(ref v, x ? 1 : 0) != 0;
public bool Or(bool x) => Interlocked.Or(ref v, x ? 1 : 0) != 0;
public bool And(bool x) => (Interlocked.And(ref v, x ? 1 : 0) & 1) == 1;
public bool Or(bool x) => (Interlocked.Or(ref v, x ? 1 : 0) & 1) == 1;
/// <returns>返回操作后的值</returns>
public bool Reverse() => (Interlocked.Increment(ref v) & 1) == 1;
public bool Xor(bool x) => (Interlocked.Add(ref v, x ? 1 : 0) & 1) == 1;
}
}

+ 382
- 113
logic/Preparation/Utility/SafeValue/InTheRange.cs View File

@@ -5,15 +5,29 @@ namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值

public class InTheVariableRange
{
protected readonly object vLock = new();
public object VLock => vLock;
private static int numOfClass = 0;
public static int NumOfClass => numOfClass;
public readonly int idInClass;
public int IdInClass => idInClass;
public InTheVariableRange()
{
idInClass = Interlocked.Increment(ref numOfClass);
}
}

/// <summary>
/// 一个保证在[0,maxValue]的可变int,支持可变的maxValue(请确保大于0)
/// </summary>
public class IntInTheVariableRange
public class IntInTheVariableRange : InTheVariableRange
{
private int v;
private int maxV;
private readonly object vLock = new();
public IntInTheVariableRange(int value, int maxValue)
#region 构造与读取
public IntInTheVariableRange(int value, int maxValue) : base()
{
if (maxValue < 0)
{
@@ -26,7 +40,7 @@ namespace Preparation.Utility
/// <summary>
/// 默认使Value=maxValue
/// </summary>
public IntInTheVariableRange(int maxValue)
public IntInTheVariableRange(int maxValue) : base()
{
if (maxValue < 0)
{
@@ -35,7 +49,7 @@ namespace Preparation.Utility
}
v = this.maxV = maxValue;
}
public IntInTheVariableRange()
public IntInTheVariableRange() : base()
{
v = this.maxV = int.MaxValue;
}
@@ -44,13 +58,34 @@ namespace Preparation.Utility
{
lock (vLock)
{
return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
return "value:" + v.ToString() + " , maxValue:" + maxV.ToString();
}
}
public int GetValue() { lock (vLock) return v; }
public static implicit operator int(IntInTheVariableRange aint) => aint.GetValue();
public int GetMaxV() { lock (vLock) return maxV; }
public (int, int) GetValueAndMaxV() { lock (vLock) return (v, maxV); }
public bool IsMaxV() { lock (vLock) return v == maxV; }
#endregion

#region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据)
public (int, long) GetValue(StartTime startTime)
{
lock (vLock)
{
return (v, startTime.Get());
}
}
public (int, int, long) GetValueAndMaxValue(StartTime startTime)
{
lock (vLock)
{
return (v, maxV, startTime.Get());
}
}
#endregion

#region 普通设置MaxV与Value的值的方法
/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
/// </summary>
@@ -82,19 +117,21 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
/// <summary>
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
public bool TrySetMaxV(int maxValue)

public int SetV(int value)
{
if (value <= 0)
{
lock (vLock)
{
return v = 0;
}
}
lock (vLock)
{
if (v > maxValue) return false;
maxV = maxValue;
return true;
return v = (value > maxV) ? maxV : value;
}
}

/// <summary>
/// 应当保证该value>=0
/// </summary>
@@ -105,20 +142,48 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
public int SetV(int value)
#endregion

#region 特殊条件的设置MaxV与Value的值的方法
/// <summary>
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
public bool TrySetMaxV(int maxValue)
{
if (value <= 0)
lock (vLock)
{
lock (vLock)
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}
public bool Set0IfNotMaxor0()
{
lock (vLock)
{
if (v < maxV && v > 0)
{
return v = 0;
v = 0;
return true;
}
}
return false;
}
public bool Set0IfMax()
{
lock (vLock)
{
return v = (value > maxV) ? maxV : value;
if (v == maxV)
{
v = 0;
return true;
}
}
return false;
}
#endregion

#region 普通运算
/// <returns>返回实际改变量</returns>
public int AddV(int addV)
{
@@ -157,6 +222,19 @@ namespace Preparation.Utility
else v *= mulV;
}
}
public void MulV(double mulV)
{
if (mulV <= 0)
{
lock (vLock) v = 0;
return;
}
lock (vLock)
{
if (v > maxV / mulV) v = maxV; //避免溢出
else v = (int)(v * mulV);
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
@@ -168,6 +246,17 @@ namespace Preparation.Utility
else v *= mulPositiveV;
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
public void MulPositiveV(double mulPositiveV)
{
lock (vLock)
{
if (v > maxV / mulPositiveV) v = maxV; //避免溢出
else v = (int)(v * mulPositiveV);
}
}
/// <returns>返回实际改变量</returns>
public int SubV(int subV)
{
@@ -193,7 +282,9 @@ namespace Preparation.Utility
}
return subPositiveV;
}
#endregion

#region 特殊条件的运算
/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// </summary>
@@ -211,50 +302,113 @@ namespace Preparation.Utility
return -1;
}
}
#endregion

public bool Set0IfNotMax()
#region 与InTheVariableRange类的运算,运算会影响该对象的值
public int AddV(IntInTheVariableRange a)
{
lock (vLock)
if (this.idInClass == a.idInClass) return -1;
bool thisLock = false;
bool thatLock = false;
try
{
if (v < maxV)
if (this.idInClass < a.idInClass)
{
v = 0;
return true;
Monitor.Enter(vLock, ref thisLock);
Monitor.Enter(a.VLock, ref thatLock);
}
else
{
Monitor.Enter(a.VLock, ref thatLock);
Monitor.Enter(vLock, ref thisLock);
}
int previousV = v;
v += a.GetValue();
if (v > maxV) v = maxV;
a.SubPositiveV(v - previousV);
return v - previousV;
}
finally
{
if (thisLock) Monitor.Exit(vLock);
if (thatLock) Monitor.Exit(a.VLock);
}
return false;
}
public bool Set0IfMax()
public int SubV(IntInTheVariableRange a)
{
lock (vLock)
if (this.idInClass == a.idInClass) return -1;
bool thisLock = false;
bool thatLock = false;
try
{
if (v == maxV)
if (this.idInClass < a.idInClass)
{
v = 0;
return true;
Monitor.Enter(vLock, ref thisLock);
Monitor.Enter(a.VLock, ref thatLock);
}
else
{
Monitor.Enter(a.VLock, ref thatLock);
Monitor.Enter(vLock, ref thisLock);
}
int previousV = v;
v -= a.GetValue();
if (v < 0) v = 0;
a.SubPositiveV(previousV - v);
return previousV - v;
}
finally
{
if (thisLock) Monitor.Exit(vLock);
if (thatLock) Monitor.Exit(a.VLock);
}
return false;
}
#endregion

public bool IsMaxV()
#region 与StartTime类的特殊条件的运算,运算会影响StartTime类的值
/// <summary>
/// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue
/// 如果无法加到maxValue则不加
/// </summary>
/// <returns>返回试图加到的值与最大值</returns>
public (int, int, long) TryAddToMaxV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
return v == maxV;
long addV = (long)(startTime.StopIfPassing(maxV - v) * speed);
if (addV < 0) return (v, maxV, startTime.Get());
if (maxV - v < addV) return (v = maxV, maxV, startTime.Get());
return ((int)(v + addV), maxV, startTime.Get());
}
}
/// <summary>
/// 增加量为时间差*速度,并将startTime变为long.MaxValue
/// </summary>
/// <returns>返回实际改变量</returns>
public int AddV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
int previousV = v;
int addV = (int)((Environment.TickCount64 - startTime.Stop()) * speed);
if (addV < 0) v += addV;
else return 0;
if (v > maxV) v = maxV;
return v - previousV;
}
}
#endregion
}

/// <summary>
/// 一个保证在[0,maxValue]的可变long,支持可变的maxValue(请确保大于0)
/// </summary>
public class LongInTheVariableRange
public class LongInTheVariableRange : InTheVariableRange
{
private long v;
private long maxV;
private readonly object vLock = new();
public LongInTheVariableRange(long value, long maxValue)
#region 构造与读取
public LongInTheVariableRange(long value, long maxValue) : base()
{
if (maxValue < 0)
{
@@ -267,7 +421,7 @@ namespace Preparation.Utility
/// <summary>
/// 默认使Value=maxValue
/// </summary>
public LongInTheVariableRange(long maxValue)
public LongInTheVariableRange(long maxValue) : base()
{
if (maxValue < 0)
{
@@ -276,7 +430,7 @@ namespace Preparation.Utility
}
v = this.maxV = maxValue;
}
public LongInTheVariableRange()
public LongInTheVariableRange() : base()
{
v = this.maxV = long.MaxValue;
}
@@ -285,13 +439,40 @@ namespace Preparation.Utility
{
lock (vLock)
{
return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
return "value:" + v.ToString() + " , maxValue:" + maxV.ToString();
}
}
public long GetValue() { lock (vLock) return v; }
public static implicit operator long(LongInTheVariableRange aint) => aint.GetValue();
public long GetMaxV() { lock (vLock) return maxV; }
public (long, long) GetValueAndMaxV() { lock (vLock) return (v, maxV); }
public bool IsMaxV()
{
lock (vLock)
{
return v == maxV;
}
}
#endregion

#region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据)
public (long, long) GetValue(StartTime startTime)
{
lock (vLock)
{
return (v, startTime.Get());
}
}
public (long, long, long) GetValueAndMaxV(StartTime startTime)
{
lock (vLock)
{
return (v, maxV, startTime.Get());
}
}
#endregion

#region 普通设置MaxV与Value的值的方法
/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
/// </summary>
@@ -323,29 +504,7 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
/// <summary>
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
public bool TrySetMaxV(long maxValue)
{
lock (vLock)
{
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}

/// <summary>
/// 应当保证该value>=0
/// </summary>
public long SetPositiveV(long value)
{
lock (vLock)
{
return v = (value > maxV) ? maxV : value;
}
}
public long SetV(long value)
{
if (value <= 0)
@@ -360,6 +519,19 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
/// <summary>
/// 应当保证该value>=0
/// </summary>
public long SetPositiveV(long value)
{
lock (vLock)
{
return v = (value > maxV) ? maxV : value;
}
}
#endregion

#region 普通运算
/// <returns>返回实际改变量</returns>
public long AddV(long addV)
{
@@ -398,6 +570,19 @@ namespace Preparation.Utility
else v *= mulV;
}
}
public void MulV(double mulV)
{
if (mulV <= 0)
{
lock (vLock) v = 0;
return;
}
lock (vLock)
{
if (v > maxV / mulV) v = maxV; //避免溢出
else v = (long)(v * mulV);
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
@@ -409,6 +594,17 @@ namespace Preparation.Utility
else v *= mulPositiveV;
}
}
/// <summary>
/// 应当保证乘数大于0
/// </summary>
public void MulPositiveV(double mulPositiveV)
{
lock (vLock)
{
if (v > maxV / mulPositiveV) v = maxV; //避免溢出
else v = (long)(v * mulPositiveV);
}
}
/// <returns>返回实际改变量</returns>
public long SubV(long subV)
{
@@ -434,22 +630,19 @@ namespace Preparation.Utility
}
return subPositiveV;
}
#endregion

#region 特殊条件的设置MaxV与Value的值的方法
/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
/// <returns>返回实际改变量</returns>
public long TryAddToMaxV(long addV)
public bool TrySetMaxV(long maxValue)
{
lock (vLock)
{
if (maxV - v <= addV)
{
addV = maxV - v;
v = maxV;
return addV;
}
return -1;
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}

@@ -477,25 +670,73 @@ namespace Preparation.Utility
}
return false;
}
#endregion

public bool IsMaxV()
#region 特殊条件的运算
/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// </summary>
/// <returns>返回实际改变量</returns>
public long TryAddToMaxV(long addV)
{
lock (vLock)
{
return v == maxV;
if (maxV - v <= addV)
{
addV = maxV - v;
v = maxV;
return addV;
}
return -1;
}
}
#endregion

#region 与StartTime类的特殊条件的运算,运算会影响StartTime类的值
/// <summary>
/// 试图加到满,如果加上时间差*速度可以达到MaxV,则加上并使startTime变为long.MaxValue
/// 如果无法加到maxValue则不加
/// </summary>
/// <returns>返回试图加到的值与最大值</returns>
public (long, long, long) TryAddToMaxV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
long addV = (long)(startTime.StopIfPassing(maxV - v) * speed);
if (addV < 0) return (v, maxV, startTime.Get());
if (maxV - v < addV) return (v = maxV, maxV, startTime.Get());
return (v + addV, maxV, startTime.Get());
}
}

/// <summary>
/// 增加量为时间差*速度,并将startTime变为long.MaxValue
/// </summary>
/// <returns>返回实际改变量</returns>
public long AddV(StartTime startTime, double speed = 1.0)
{
lock (vLock)
{
long previousV = v;
long addV = (Environment.TickCount64 - startTime.Stop());
if (addV < 0) v += (long)(addV * speed);
else return 0;
if (v > maxV) v = maxV;
return v - previousV;
}
}
#endregion
}

/// <summary>
/// 一个保证在[0,maxValue]的可变double,支持可变的maxValue(请确保大于0)
/// </summary>
public class DoubleInTheVariableRange
public class DoubleInTheVariableRange : InTheVariableRange
{
private double v;
private double maxV;
private readonly object vLock = new();
public DoubleInTheVariableRange(double value, double maxValue)
#region 构造与读取
public DoubleInTheVariableRange(double value, double maxValue) : base()
{
if (maxValue < 0)
{
@@ -508,7 +749,7 @@ namespace Preparation.Utility
/// <summary>
/// 默认使Value=maxValue
/// </summary>
public DoubleInTheVariableRange(double maxValue)
public DoubleInTheVariableRange(double maxValue) : base()
{
if (maxValue < 0)
{
@@ -517,7 +758,7 @@ namespace Preparation.Utility
}
v = this.maxV = maxValue;
}
public DoubleInTheVariableRange()
public DoubleInTheVariableRange() : base()
{
v = this.maxV = double.MaxValue;
}
@@ -526,13 +767,40 @@ namespace Preparation.Utility
{
lock (vLock)
{
return "value:" + v.ToString() + " ,maxValue:" + maxV.ToString();
return "value:" + v.ToString() + " , maxValue:" + maxV.ToString();
}
}
public double GetValue() { lock (vLock) return v; }
public static implicit operator double(DoubleInTheVariableRange adouble) => adouble.GetValue();
public double GetMaxV() { lock (vLock) return maxV; }
public (double, double) GetValueAndMaxV() { lock (vLock) return (v, maxV); }
public bool IsMaxV()
{
lock (vLock)
{
return v == maxV;
}
}
#endregion

#region 内嵌读取(在锁的情况下读取内容同时读取其他更基本的外部数据)
public (double, long) GetValue(StartTime startTime)
{
lock (vLock)
{
return (v, startTime.Get());
}
}
public (double, double, long) GetValueAndMaxValue(StartTime startTime)
{
lock (vLock)
{
return (v, maxV, startTime.Get());
}
}
#endregion

#region 普通设置MaxV与Value的值的方法
/// <summary>
/// 若maxValue<=0则maxValue设为0并返回False
/// </summary>
@@ -564,29 +832,7 @@ namespace Preparation.Utility
if (v > maxValue) v = maxValue;
}
}
/// <summary>
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
public bool TrySetMaxV(double maxValue)
{
lock (vLock)
{
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}

/// <summary>
/// 应当保证该value>=0
/// </summary>
public double SetPositiveV(double value)
{
lock (vLock)
{
return v = (value > maxV) ? maxV : value;
}
}
public double SetV(double value)
{
if (value <= 0)
@@ -601,6 +847,19 @@ namespace Preparation.Utility
return v = (value > maxV) ? maxV : value;
}
}
/// <summary>
/// 应当保证该value>=0
/// </summary>
public double SetPositiveV(double value)
{
lock (vLock)
{
return v = (value > maxV) ? maxV : value;
}
}
#endregion

#region 普通运算
/// <returns>返回实际改变量</returns>
public double AddV(double addV)
{
@@ -675,22 +934,19 @@ namespace Preparation.Utility
}
return subPositiveV;
}
#endregion

#region 特殊条件的设置MaxV与Value的值的方法
/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// 如果当前值大于maxValue,则更新maxValue失败
/// </summary>
/// <returns>返回实际改变量</returns>
public double TryAddToMaxV(double addV)
public bool TrySetMaxV(double maxValue)
{
lock (vLock)
{
if (maxV - v <= addV)
{
addV = maxV - v;
v = maxV;
return addV;
}
return -1;
if (v > maxValue) return false;
maxV = maxValue;
return true;
}
}

@@ -718,13 +974,26 @@ namespace Preparation.Utility
}
return false;
}
#endregion

public bool IsMaxV()
#region 特殊条件的运算
/// <summary>
/// 试图加到满,如果无法加到maxValue则不加并返回-1
/// </summary>
/// <returns>返回实际改变量</returns>
public double TryAddToMaxV(double addV)
{
lock (vLock)
{
return v == maxV;
if (maxV - v <= addV)
{
addV = maxV - v;
v = maxV;
return addV;
}
return -1;
}
}
#endregion
}
}

+ 117
- 8
logic/Preparation/Utility/SafeValue/TimeBased.cs View File

@@ -5,23 +5,53 @@ namespace Preparation.Utility
{
//其对应属性不应当有set访问器,避免不安全的=赋值

/// <summary>
/// 记录上次Start的时间,尚未Start则为long.MaxValue
/// 当前不为long.MaxValue则不能Start
/// </summary>
public class StartTime
{
private long _time;
public StartTime(long time)
{
_time = time;
}
public StartTime() { _time = long.MaxValue; }
public long Get() => Interlocked.CompareExchange(ref _time, -2, -2);
public override string ToString() => Get().ToString();
/// <returns>返回操作前的值</returns>
public long Start() => Interlocked.CompareExchange(ref _time, Environment.TickCount64, long.MaxValue);
/// <returns>返回操作前的值</returns>
public long Stop() => Interlocked.Exchange(ref _time, long.MaxValue);
/// <returns>返回时间差,<0意味着未开始</returns>
public long StopIfPassing(long passedTime)
{
long ans = Environment.TickCount64 - Interlocked.CompareExchange(ref _time, -2, -2);
if (ans > passedTime)
{
Interlocked.Exchange(ref _time, long.MaxValue);
}
return ans;
}
}

/// <summary>
/// 根据时间推算Start后完成多少进度的进度条(long)。
/// 只允许Start(清零状态的进度条才可以Start)时修改needTime(请确保大于0);
/// 支持InterruptToSet0使未完成的进度条终止清零;支持Set0使进度条强制终止清零;
/// 通过原子操作实现。
/// </summary>
public class TimeBasedProgressForInterrupting
public class TimeBasedProgressOptimizedForInterrupting
{
private long endT = long.MaxValue;
private long needT;

public TimeBasedProgressForInterrupting(long needTime)
public TimeBasedProgressOptimizedForInterrupting(long needTime)
{
if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressForInterrupting.needTime (" + needTime.ToString() + ") is less than 0.");
if (needTime <= 0) Debugger.Output("Bug:TimeBasedProgressOptimizedForInterrupting.needProgress (" + needTime.ToString() + ") is less than 0.");
this.needT = needTime;
}
public TimeBasedProgressForInterrupting()
public TimeBasedProgressOptimizedForInterrupting()
{
this.needT = 0;
}
@@ -32,7 +62,7 @@ namespace Preparation.Utility
{
return Interlocked.CompareExchange(ref endT, -2, -2) <= Environment.TickCount64;
}
public bool IsOpened() => Interlocked.Read(ref endT) != long.MaxValue;
public bool IsStarted() => Interlocked.Read(ref endT) != long.MaxValue;
/// <summary>
/// GetProgress<0则表明未开始
/// </summary>
@@ -68,7 +98,7 @@ namespace Preparation.Utility
/// <summary>
/// <0则表明未开始
/// </summary>
public static implicit operator long(TimeBasedProgressForInterrupting pLong) => pLong.GetProgress();
public static implicit operator long(TimeBasedProgressOptimizedForInterrupting pLong) => pLong.GetProgress();

/// <summary>
/// GetProgressDouble<0则表明未开始
@@ -94,12 +124,12 @@ namespace Preparation.Utility
{
if (needTime <= 0)
{
Debugger.Output("Warning:Start TimeBasedProgressForInterrupting with the needTime (" + needTime.ToString() + ") which is less than 0.");
Debugger.Output("Warning:Start TimeBasedProgressOptimizedForInterrupting with the needProgress (" + needTime.ToString() + ") which is less than 0.");
return false;
}
//规定只有Start可以修改needT,且需要先访问endTime,从而避免锁(某种程度上endTime可以认为是needTime的锁)
if (Interlocked.CompareExchange(ref endT, Environment.TickCount64 + needTime, long.MaxValue) != long.MaxValue) return false;
if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressForInterrupting is " + needTime.ToString() + ",which is too small.");
if (needTime <= 2) Debugger.Output("Warning:the field of TimeBasedProgressOptimizedForInterrupting is " + needTime.ToString() + ",which is too small.");
Interlocked.Exchange(ref needT, needTime);
return true;
}
@@ -128,6 +158,85 @@ namespace Preparation.Utility
//增加其他新的写操作可能导致不安全
}

public class TimeBasedProgressAtVariableSpeed
{
public LongInTheVariableRange progress;
public StartTime startTime = new();
public AtomicDouble speed;

#region 构造
public TimeBasedProgressAtVariableSpeed(long needProgress, double speed)
{
if (needProgress <= 0) Debugger.Output("Bug:TimeBasedProgressAtVariableSpeed.needProgress (" + needProgress.ToString() + ") is less than 0.");
this.progress = new(0, needProgress);
this.speed = new(speed);
}
public TimeBasedProgressAtVariableSpeed()
{
this.progress = new(0, 0);
this.speed = new(1.0);
}
#endregion

#region 读取
public override string ToString()
{
return "ProgressStored: " + progress.ToString()
+ " ; LastStartTime: " + startTime.ToString() + "ms"
+ " ; Speed: " + speed.ToString();
}
public long GetProgressNow() => progress.TryAddToMaxV(startTime, (double)speed).Item1;
public (long, long, long) GetProgressNowAndNeedTimeAndLastStartTime() => progress.TryAddToMaxV(startTime, (double)speed);
public long GetProgressStored() => progress.GetValue();
public (long, long) GetProgressStoredAndNeedTime() => progress.GetValueAndMaxV();
public (long, long, long) GetProgressStoredAndNeedTimeAndLastStartTime() => progress.GetValueAndMaxV(startTime);

public bool IsFinished()
{
long progressNow, needTime;
(progressNow, needTime, _) = progress.TryAddToMaxV(startTime);
return progressNow == needTime;
}
public bool IsProgressing()
{
long progressNow, needTime, startT;
(progressNow, needTime, startT) = progress.TryAddToMaxV(startTime);
return (startT != long.MaxValue && progressNow != needTime);
}
#endregion

public bool Start(long needTime)
{
if (needTime <= 2)
{
Debugger.Output("Warning:Start TimeBasedProgressAtVariableSpeed with the needProgress (" + needTime.ToString() + ") which is less than 0.");
return false;
}
if (startTime.Start() != long.MaxValue) return false;
progress.SetMaxV(needTime);
return true;
}
public bool Start()
{
return startTime.Start() == long.MaxValue;
}
/// <summary>
/// 使进度条强制终止清零
/// </summary>
public void Set0()
{
startTime.Stop();
progress.SetPositiveV(0);
}
/// <summary>
/// 使进度条暂停
/// </summary>
public bool Pause()
{
return progress.AddV(startTime, (double)speed) != 0;
}
}

/// <summary>
/// 冷却时间为可变的CDms的bool,不支持查看当前进度,初始为True
/// </summary>


Loading…
Cancel
Save