Skip to content

Commit fa4448c

Browse files
Add wip chat resource
1 parent 031990d commit fa4448c

File tree

23 files changed

+275
-103
lines changed

23 files changed

+275
-103
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ matrix:
4949
- cd ..
5050
- cd ..
5151
- echo "${TRAVIS_BUILD_NUMBER}"
52-
- powershell move runtime/build/src/Release/csharp-module.dll upload/modules/csharp-module.dll
52+
- powershell move runtime/build/src/Release/csharp-module.dll upload/modules
5353
- cd upload && cd modules && ls
5454
- cd .. && cd ..
5555
- dist: xenial

Dockerfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ RUN cd AltV.Net.Example && dotnet publish -c Release
2828

2929
RUN cd AltV.Net.Host && dotnet publish -c Release
3030

31+
RUN cd AltV.Net.Chat && dotnet publish -c Release
32+
3133
#FROM debian:stable
3234
FROM ubuntu:18.04
3335

@@ -49,10 +51,12 @@ COPY startgdb.sh .
4951
COPY libnode.so.64 .
5052
COPY libnode-module.so modules/
5153
COPY resource.cfg resources/example/
54+
COPY --from=dotnet /altv-example/AltV.Net.Chat/resource.cfg resources/chat/
5255
COPY data/ ./data
53-
COPY chat/ resources/chat
56+
#COPY chat/ resources/chat
5457
COPY --from=clang /runtime/build/src/libcsharp-module.so modules/
5558
COPY --from=dotnet /altv-example/AltV.Net.Example/bin/Release/netcoreapp3.0/publish resources/example/
59+
COPY --from=dotnet /altv-example/AltV.Net.Chat/bin/Release/netcoreapp3.0/publish resources/chat/
5660
COPY --from=dotnet /altv-example/AltV.Net.Host/bin/Release/netcoreapp3.0/publish .
5761
COPY --from=dotnet /usr/share/dotnet /usr/share/dotnet
5862
RUN ls -l

api/AltV.Net.Async/AltV.Net.Async.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
<RepositoryUrl>https://github.com/altmp-csharp/docs</RepositoryUrl>
1313
<RepositoryType>git</RepositoryType>
1414
<PackageTags>altv gta bridge</PackageTags>
15-
<PackageVersion>1.12.3-beta</PackageVersion>
15+
<PackageVersion>1.13.0-alpha</PackageVersion>
1616
<PackageLicenseFile>license.txt</PackageLicenseFile>
1717
<PackageReleaseNotes>Add IScript for automatically loading scripts that contains Events and ScriptEvents
1818
Update dependencies</PackageReleaseNotes>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<PackageVersion>1.0.0-dev-preview</PackageVersion>
6+
<PackageLicenseFile>license.txt</PackageLicenseFile>
7+
<PackageReleaseNotes>Initial commit</PackageReleaseNotes>
8+
</PropertyGroup>
9+
10+
<ItemGroup>
11+
<None Include="license\license.txt" Pack="true" PackagePath="" />
12+
</ItemGroup>
13+
14+
<ItemGroup>
15+
<ProjectReference Include="..\AltV.Net\AltV.Net.csproj" />
16+
</ItemGroup>
17+
</Project>

api/AltV.Net.Chat/Chat.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.Linq;
3+
using AltV.Net.Elements.Entities;
4+
using AltV.Net.Native;
5+
6+
namespace AltV.Net.Chat
7+
{
8+
public class Chat : Resource
9+
{
10+
private static Action<string, Action<IPlayer, string, string[]>> registerCmd;
11+
12+
public override void OnStart()
13+
{
14+
Alt.On<IPlayer, string>("chatmessage", OnChatMessage, OnChatMessageParser);
15+
16+
registerCmd = delegate(string s, Action<IPlayer, string, string[]> action)
17+
{
18+
CommandHandlers.Add(s, action);
19+
};
20+
21+
Alt.Export<string, Action<IPlayer, string, string[]>>("registerCmd", CommandHandlers.Add);
22+
Alt.Export("broadcast", delegate(string message) { ChatUtils.SendBroadcastChatMessage(message); });
23+
Alt.Export("send", delegate(IPlayer player, string message) { player.SendChatMessage(message); });
24+
}
25+
26+
public override void OnStop()
27+
{
28+
}
29+
30+
private void OnChatMessage(IPlayer player, string message)
31+
{
32+
if (string.IsNullOrEmpty(message)) return;
33+
if (message.StartsWith("/"))
34+
{
35+
message = message.Trim().Remove(0, 1);
36+
if (message.Length > 0)
37+
{
38+
Alt.Log("[chat:cmd] " + player.Name + ": /" + message);
39+
var args = message.Split(' ');
40+
var cmd = args[0];
41+
CommandHandlers.InvokeCmd(player, cmd, args.Skip(1).ToArray());
42+
}
43+
}
44+
else
45+
{
46+
message = message.Trim();
47+
if (message.Length > 0)
48+
{
49+
Alt.Log("[chat:msg] " + player.Name + ": " + message);
50+
51+
player.Emit("chatmessage", player.Name, message);
52+
}
53+
}
54+
}
55+
56+
private void OnChatMessageParser(IPlayer player, ref MValueArray mValueArray,
57+
Action<IPlayer, string> func)
58+
{
59+
if (mValueArray.Size != 1) return;
60+
var reader = mValueArray.Reader();
61+
if (!reader.GetNext(out string message)) return;
62+
/*if (!reader.GetNext(out MValueArray mValueArgs)) return;
63+
var argsCount = (int) mValueArgs.Size;
64+
var args = new string[argsCount];
65+
var argsReader = mValueArgs.Reader();
66+
for (var i = 0; i < argsCount; i++)
67+
{
68+
if (!argsReader.GetNext(out string currArg)) return;
69+
args[i] = currArg;
70+
}*/
71+
72+
func(player, message);
73+
}
74+
75+
~Chat()
76+
{
77+
Console.WriteLine("chat deinit");
78+
}
79+
}
80+
}

api/AltV.Net.Chat/ChatUtils.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
namespace AltV.Net.Chat
2+
{
3+
public static class ChatUtils
4+
{
5+
public static void SendBroadcastChatMessage(string message)
6+
{
7+
Alt.EmitAllClients("chatmessage", null, message);
8+
}
9+
}
10+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using AltV.Net.Elements.Entities;
4+
5+
namespace AltV.Net.Chat
6+
{
7+
public static class CommandHandlers
8+
{
9+
private static readonly IDictionary<string, HashSet<Action<IPlayer, string, string[]>>> commandHandlers =
10+
new Dictionary<string, HashSet<Action<IPlayer, string, string[]>>>();
11+
12+
public static void Add(string command, Action<IPlayer, string, string[]> handler)
13+
{
14+
if (!commandHandlers.TryGetValue(command, out var handlers))
15+
{
16+
handlers = new HashSet<Action<IPlayer, string, string[]>>();
17+
commandHandlers[command] = handlers;
18+
}
19+
20+
handlers.Add(handler);
21+
}
22+
23+
public static void InvokeCmd(IPlayer player, string cmd, string[] args)
24+
{
25+
if (!commandHandlers.TryGetValue(cmd, out var handlers))
26+
{
27+
player.SendChatMessage("{FF0000} Unknown command /" + cmd + "");
28+
return;
29+
}
30+
31+
foreach (var handler in handlers)
32+
{
33+
handler(player, cmd, args);
34+
}
35+
}
36+
}
37+
}

api/AltV.Net.Chat/PlayerChat.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using AltV.Net.Elements.Entities;
2+
3+
namespace AltV.Net.Chat
4+
{
5+
public static class PlayerChat
6+
{
7+
public static void SendChatMessage(this IPlayer player, string message)
8+
{
9+
player.Emit("chatmessage", null, message);
10+
}
11+
}
12+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2019 alt:mp
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

api/AltV.Net.Chat/resource.cfg

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
type: csharp,
2+
main: AltV.Net.Chat.dll

0 commit comments

Comments
 (0)