forked from Bob74/iFruitAddon2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiFruitContact.cs
218 lines (193 loc) · 8.44 KB
/
iFruitContact.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.IO;
using GTA.Native;
using GTA;
namespace iFruitAddon2
{
public class iFruitContact
{
private bool _dialActive, _busyActive;
private int _dialSoundID = -1;
private int _busySoundID = -1;
private int _callTimer, _busyTimer;
/// <summary>
/// Fired when the contact picks up the phone.
/// </summary>
public event ContactAnsweredEvent Answered;
protected virtual void OnAnswered(iFruitContact sender) { Answered?.Invoke(this); }
/// <summary>
/// The name of the contact.
/// </summary>
public string Name { get; set; } = "";
/// <summary>
/// The index where we should draw the item.
/// </summary>
public int Index { get; private set; } = 0;
/// <summary>
/// Status representing the outcome when the contact is called.
/// Contact will answer when true.
/// </summary>
public bool Active { get; set; } = true;
/// <summary>
/// Milliseconds timeout before the contact picks up.
/// Set this to 0 if you want the contact to answer instantly.
/// </summary>
public int DialTimeout { get; set; } = 0;
/// <summary>
/// The icon to associate with this contact.
/// </summary>
public ContactIcon Icon { get; set; } = ContactIcon.Generic;
/// <summary>
/// Set the contact text in bold.
/// </summary>
public bool Bold { get; set; } = false;
public iFruitContact(string name)
{
UpdateContactIndex();
Name = name;
Index = iFruitAddon2.ContactIndex;
iFruitAddon2.ContactIndex++;
}
internal void Draw(int handle)
{
Function.Call(Hash._PUSH_SCALEFORM_MOVIE_FUNCTION, handle, "SET_DATA_SLOT");
Function.Call(Hash._PUSH_SCALEFORM_MOVIE_FUNCTION_PARAMETER_INT, 2);
Function.Call(Hash._PUSH_SCALEFORM_MOVIE_FUNCTION_PARAMETER_INT, Index);
Function.Call(Hash._PUSH_SCALEFORM_MOVIE_FUNCTION_PARAMETER_INT, 0);
Function.Call(Hash._BEGIN_TEXT_COMPONENT, "STRING");
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, Name);
Function.Call(Hash._END_TEXT_COMPONENT);
Function.Call(Hash._BEGIN_TEXT_COMPONENT, "CELL_999");
Function.Call(Hash._END_TEXT_COMPONENT);
Function.Call(Hash._BEGIN_TEXT_COMPONENT, "CELL_2000");
Function.Call(Hash._ADD_TEXT_COMPONENT_STRING, Icon.Name.SetBold(Bold));
Function.Call(Hash._END_TEXT_COMPONENT);
Function.Call(Hash._POP_SCALEFORM_MOVIE_FUNCTION_VOID);
}
internal void Update()
{
// Contact was busy and busytimer has ended
if (_busyActive && Game.GameTime > _busyTimer)
{
Game.Player.Character.Task.PutAwayMobilePhone();
Function.Call(Hash.STOP_SOUND, _busySoundID);
Function.Call(Hash.RELEASE_SOUND_ID, _busySoundID);
_busySoundID = -1;
_busyActive = false;
}
// We are calling the contact
if (_dialActive && Game.GameTime > _callTimer)
{
Function.Call(Hash.STOP_SOUND, _dialSoundID);
Function.Call(Hash.RELEASE_SOUND_ID, _dialSoundID);
_dialSoundID = -1;
if (!Active)
{
// Contact is busy, play the busy sound until the busytimer runs off
iFruitContactCollection.DisplayCallUI(CustomiFruit.GetCurrentInstance().Handle, Name, "CELL_220", Icon.Name.SetBold(Bold)); // Displays "BUSY"
_busySoundID = Function.Call<int>(Hash.GET_SOUND_ID);
Function.Call(Hash.PLAY_SOUND_FRONTEND, _busySoundID, "Remote_Engaged", "Phone_SoundSet_Default", 1);
_busyTimer = Game.GameTime + 5000;
_busyActive = true;
}
else
{
iFruitContactCollection.DisplayCallUI(CustomiFruit.GetCurrentInstance().Handle, Name, "CELL_219", Icon.Name.SetBold(Bold)); // Displays "CONNECTED"
OnAnswered(this); // Answer the phone
}
_dialActive = false;
}
}
/// <summary>
/// Call this contact.
/// If DialTimeout less or equal than 0, the contact will pickup instantly.
/// </summary>
public void Call()
{
// Cannot call if already on call or contact is busy (Active == false)
if (_dialActive || _busyActive)
return;
Game.Player.Character.Task.UseMobilePhone();
// Do we have to wait before the contact pickup the phone?
if (DialTimeout > 0)
{
// Play the Dial sound
iFruitContactCollection.DisplayCallUI(CustomiFruit.GetCurrentInstance().Handle, Name, "CELL_220", Icon.Name.SetBold(Bold)); // Displays "BUSY"
_dialSoundID = Function.Call<int>(Hash.GET_SOUND_ID);
Function.Call(Hash.PLAY_SOUND_FRONTEND, _dialSoundID, "Dial_and_Remote_Ring", "Phone_SoundSet_Default", 1);
_callTimer = Game.GameTime + DialTimeout;
_dialActive = true;
}
else
{
iFruitContactCollection.DisplayCallUI(CustomiFruit.GetCurrentInstance().Handle, Name, "CELL_219", Icon.Name.SetBold(Bold)); // Displays "CONNECTED"
OnAnswered(this); // Answer the phone instantly
}
}
/// <summary>
/// Stop and release phone sounds.
/// </summary>
public void EndCall()
{
if (_dialActive)
{
Function.Call(Hash.STOP_SOUND, _dialSoundID);
Function.Call(Hash.RELEASE_SOUND_ID, _dialSoundID);
_dialSoundID = -1;
_dialActive = false;
}
if (_busyActive)
{
Function.Call(Hash.STOP_SOUND, _busySoundID);
Function.Call(Hash.RELEASE_SOUND_ID, _busySoundID);
_busySoundID = -1;
_busyActive = false;
}
}
private void UpdateContactIndex()
{
// Warning: new iFruitContact(...) can be called before iFruitAddon2 is initialized.
// That's why it is important not to rely on static variables inside the iFruitAddon2 class.
string tempFile = iFruitAddon2.GetTempFilePath();
if (File.Exists(tempFile))
{
// Not the first launch
bool written = false;
while (!written)
{
try
{
// We need to check if the file is unlocked and then get the value and write the new one.
int index;
StreamReader sr = new StreamReader(tempFile);
if (int.TryParse(sr.ReadLine().Trim(new char[] { '\r', '\n', ' ' }), out index))
iFruitAddon2.ContactIndex = index;
sr.Close();
StreamWriter file = new StreamWriter(tempFile);
file.WriteLine(iFruitAddon2.ContactIndex + 1);
file.Close();
written = true;
}
catch (IOException)
{
// The file is locked when StreamReader or StreamWriter has opend it.
// The current instance of iFruitAddon2 must wait until the file is released.
}
catch (Exception ex)
{
// Unknown error occured
Logger.Log(ex.Message);
written = true;
}
}
}
else
{
// First launch. We create the file
StreamWriter file = new StreamWriter(tempFile);
file.Write(iFruitAddon2.ContactIndex + 1);
file.Close();
}
}
}
}