Skip to content

Commit bf331b3

Browse files
author
“寧々”
committed
First Commit
1 parent 71eaf0e commit bf331b3

37 files changed

+2893
-0
lines changed

MiniCarLib.sln

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
# Visual Studio Version 16
4+
VisualStudioVersion = 16.0.30907.101
5+
MinimumVisualStudioVersion = 10.0.40219.1
6+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniCarLib", "MiniCarLib\MiniCarLib.csproj", "{B03A865D-F1B5-4297-80BD-06CE1BC9F8A6}"
7+
EndProject
8+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MiniCarLibTester", "MiniCarLibTester\MiniCarLibTester.csproj", "{D1862CB4-206B-4DFA-B396-7F39FD9FCA39}"
9+
EndProject
10+
Global
11+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
12+
Debug|Any CPU = Debug|Any CPU
13+
Release|Any CPU = Release|Any CPU
14+
EndGlobalSection
15+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
16+
{B03A865D-F1B5-4297-80BD-06CE1BC9F8A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
17+
{B03A865D-F1B5-4297-80BD-06CE1BC9F8A6}.Debug|Any CPU.Build.0 = Debug|Any CPU
18+
{B03A865D-F1B5-4297-80BD-06CE1BC9F8A6}.Release|Any CPU.ActiveCfg = Release|Any CPU
19+
{B03A865D-F1B5-4297-80BD-06CE1BC9F8A6}.Release|Any CPU.Build.0 = Release|Any CPU
20+
{D1862CB4-206B-4DFA-B396-7F39FD9FCA39}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
21+
{D1862CB4-206B-4DFA-B396-7F39FD9FCA39}.Debug|Any CPU.Build.0 = Debug|Any CPU
22+
{D1862CB4-206B-4DFA-B396-7F39FD9FCA39}.Release|Any CPU.ActiveCfg = Release|Any CPU
23+
{D1862CB4-206B-4DFA-B396-7F39FD9FCA39}.Release|Any CPU.Build.0 = Release|Any CPU
24+
EndGlobalSection
25+
GlobalSection(SolutionProperties) = preSolution
26+
HideSolutionNode = FALSE
27+
EndGlobalSection
28+
GlobalSection(ExtensibilityGlobals) = postSolution
29+
SolutionGuid = {627AC0C5-01CA-47FF-A9A2-16D2417F660D}
30+
EndGlobalSection
31+
EndGlobal

MiniCarLib/CarList.cs

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
using System.Collections.Concurrent;
7+
8+
using MiniCarLib.Core;
9+
10+
namespace MiniCarLib
11+
{
12+
public class CarList
13+
{
14+
Dictionary<int,ICar> Cars { get; } = new Dictionary<int, ICar>();
15+
object locker = new object();
16+
17+
public bool AddCar(ICar car)
18+
{
19+
lock(locker)
20+
{
21+
if (Cars.ContainsKey(car.ID))
22+
return false;
23+
Cars.Add(car.ID,car);
24+
return true;
25+
}
26+
}
27+
28+
public void AddorAlterCar(ICar car)
29+
{
30+
lock (locker)
31+
{
32+
if (Cars.ContainsKey(car.ID))
33+
Cars[car.ID] = car;
34+
else
35+
Cars.Add(car.ID, car);
36+
}
37+
}
38+
39+
public bool RemoveCar(ICar car)
40+
{
41+
lock (locker)
42+
{
43+
if (!Cars.ContainsKey(car.ID))
44+
return false;
45+
Cars.Remove(car.ID);
46+
return true;
47+
}
48+
}
49+
50+
51+
public int GetAvailibleID(int max = int.MaxValue, int serverid = 0x0000)
52+
{
53+
for (int i = 1; i < max; i++)
54+
if (!Cars.ContainsKey(i) && i != serverid)
55+
return i;
56+
return 0;
57+
}
58+
59+
public ICar this[int id]
60+
{
61+
get
62+
{
63+
lock(locker)
64+
{
65+
ICar rst;
66+
bool ok = Cars.TryGetValue(id, out rst);
67+
if (ok)
68+
return rst;
69+
else
70+
return null;
71+
}
72+
}
73+
}
74+
}
75+
}

MiniCarLib/Class1.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib
8+
{
9+
public class Class1
10+
{
11+
}
12+
}

MiniCarLib/Core/CarFactory.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public enum CarType
10+
{
11+
Unkown = 0,
12+
QianCar = 1
13+
}
14+
public static class CarFactory
15+
{
16+
public static ICar CreateInstance(CarType type)
17+
{
18+
switch(type)
19+
{
20+
case CarType.QianCar:return new QianCar();
21+
default:throw new Exception("No such type of car!");
22+
}
23+
}
24+
}
25+
}

MiniCarLib/Core/ComDataFactory.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public class QianComDataFactory
10+
{
11+
public static QianComData CreateInstance(DataFunctionType fun)
12+
{
13+
switch(fun)
14+
{
15+
case DataFunctionType.RegisterRequest: return new RegisterRequestData();
16+
case DataFunctionType.RegisterResponse: return new RegisterResponseData();
17+
case DataFunctionType.RequestCarState: return new RequestCarStateData();
18+
case DataFunctionType.ReportCarState: return new ReportCarStateData();
19+
case DataFunctionType.SetCarRoute: return new SetCarRouteData();
20+
case DataFunctionType.ApplyForEnter:return new ApplyForEnterData();
21+
case DataFunctionType.CallCarEnter:return new CallCarEnterData();
22+
case DataFunctionType.CarEnterConfirm:return new CarEnterConfirmData();
23+
case DataFunctionType.ApplyForLeave:return new ApplyForLeaveData();
24+
case DataFunctionType.CallCarLeave:return new CallCarLeaveData();
25+
case DataFunctionType.CarLeaveConfirm:return new CarLeaveConfirmData();
26+
case DataFunctionType.UnregisterRequest:return new UnregisterRequestData();
27+
case DataFunctionType.UnregisterResponse:return new UnregisterResponseData();
28+
case DataFunctionType.CustomData:return new CustomData();
29+
default:return null;
30+
}
31+
}
32+
33+
34+
}
35+
}

MiniCarLib/Core/ComDataReader.cs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public class ComDataReader
10+
{
11+
byte[] buffer;
12+
int offset;
13+
int ptr = 0;
14+
public ComDataReader(byte[] arr,int offset)
15+
{
16+
buffer = arr;
17+
this.offset = offset;
18+
ptr = offset;
19+
}
20+
21+
public int Pointer => ptr;
22+
23+
public void Seek(int index)
24+
{
25+
ptr = index;
26+
}
27+
28+
public void SeekRelevant(int index)
29+
{
30+
ptr += index;
31+
}
32+
33+
public void SeekToStart()
34+
{
35+
ptr = offset;
36+
}
37+
38+
public bool ReadBoolean()
39+
{
40+
return buffer[ptr++] == 0 ? false : true;
41+
}
42+
43+
public byte ReadByte()
44+
{
45+
return buffer[ptr++];
46+
}
47+
48+
public ushort ReadHalfWord()
49+
{
50+
return Util.BytesToHalfword(buffer[ptr++], buffer[ptr++]);
51+
}
52+
53+
public uint ReadWord()
54+
{
55+
return (uint)(buffer[ptr++] << 24 | buffer[ptr++] << 16 | buffer[ptr++] << 8 | buffer[ptr++]);
56+
}
57+
58+
public void ReadByteArray(byte[] buff,int offset,int len)
59+
{
60+
Array.Copy(buffer, ptr, buff, offset, len);
61+
ptr += len;
62+
}
63+
64+
public string ReadString(int len,Encoding en = null)
65+
{
66+
if (en is null)
67+
en = Encoding.ASCII;
68+
string rt = en.GetString(buffer, ptr, len);
69+
ptr += len;
70+
return rt;
71+
}
72+
73+
public T ReadEnum<T>() where T : Enum
74+
{
75+
return (T)(object)buffer[ptr++];
76+
}
77+
}
78+
}

MiniCarLib/Core/ComDataWriter.cs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public class ComDataWriter
10+
{
11+
byte[] buffer;
12+
int offset;
13+
int ptr = 0;
14+
public ComDataWriter(byte[] arr, int offset)
15+
{
16+
buffer = arr;
17+
this.offset = offset;
18+
ptr = offset;
19+
}
20+
public int Pointer => ptr;
21+
22+
public void Seek(int index)
23+
{
24+
ptr = index;
25+
}
26+
27+
public void SeekRelevant(int index)
28+
{
29+
ptr += index;
30+
}
31+
32+
public void SeekToStart()
33+
{
34+
ptr = offset;
35+
}
36+
public void WriteBoolean(bool b)
37+
{
38+
buffer[ptr++] = (byte)(b ? 1 : 0);
39+
}
40+
41+
public void WriteByte(byte b)
42+
{
43+
buffer[ptr++] = b;
44+
}
45+
46+
public void WriteHalfWord(ushort i)
47+
{
48+
buffer[ptr++] = (byte)(i >> 8);
49+
buffer[ptr++] = (byte)(i & 0xFF);
50+
}
51+
52+
public void WriteWord(uint i)
53+
{
54+
buffer[ptr++] = (byte)(i >> 24);
55+
buffer[ptr++] = (byte)((i & 0x00FF0000) >> 16);
56+
buffer[ptr++] = (byte)((i & 0x0000FF00) >> 8);
57+
buffer[ptr++] = (byte)(i & 0x000000FF);
58+
}
59+
60+
public void WriteByteArray(byte[] buff, int offset, int len)
61+
{
62+
Array.Copy(buff, offset, buffer, ptr, len);
63+
ptr += len;
64+
}
65+
66+
public void WriteString(string s, Encoding en = null)
67+
{
68+
if (en is null)
69+
en = Encoding.ASCII;
70+
byte[] temp = en.GetBytes(s);
71+
Array.Copy(temp, 0, buffer, ptr, temp.Length);
72+
ptr += temp.Length;
73+
}
74+
75+
public void WriteEnum<T>(T e) where T : Enum
76+
{
77+
buffer[ptr++] = (byte)(object)e;
78+
}
79+
}
80+
}

MiniCarLib/Core/ICar.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public interface ICar
10+
{
11+
CarType carType { get; }
12+
int ID { get; set; }
13+
IComClient ComClient { get; set; }
14+
}
15+
}

MiniCarLib/Core/ICarMap.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace MiniCarLib.Core
8+
{
9+
public interface ICarMap<T,K> where T : IMapPoint<K> where K : IMapRoute
10+
{
11+
Dictionary<string, T> Points { get; }
12+
T this[string name] { get; }
13+
}
14+
}

0 commit comments

Comments
 (0)