forked from EvilBeaver/OneScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPropertyValueReference.cs
More file actions
72 lines (63 loc) · 2.54 KB
/
PropertyValueReference.cs
File metadata and controls
72 lines (63 loc) · 2.54 KB
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
/*----------------------------------------------------------
This Source Code Form is subject to the terms of the
Mozilla Public License, v.2.0. If a copy of the MPL
was not distributed with this file, You can obtain one
at http://mozilla.org/MPL/2.0/.
----------------------------------------------------------*/
using System;
using OneScript.Contexts;
using OneScript.Exceptions;
using ScriptEngine.Machine;
namespace OneScript.Values
{
public class PropertyValueReference : IValueReference
{
private readonly IRuntimeContextInstance _context;
private readonly int _contextPropertyNumber;
public PropertyValueReference(IRuntimeContextInstance context, string propertyName)
{
_context = context;
_contextPropertyNumber = context.GetPropertyNumber(propertyName);
}
public PropertyValueReference(IRuntimeContextInstance context, int propertyId)
{
_context = context;
_contextPropertyNumber = propertyId;
}
public bool Equals(IValueReference other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
if (!(other is PropertyValueReference prop))
return false;
return _context.Equals(prop._context) && _contextPropertyNumber == prop._contextPropertyNumber;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
return obj.GetType() == this.GetType() && Equals((PropertyValueReference)obj);
}
public override int GetHashCode()
{
return HashCode.Combine(_context, _contextPropertyNumber);
}
public IValue Value
{
get
{
if (_context.IsPropReadable(_contextPropertyNumber))
return (BslValue)_context.GetPropValue(_contextPropertyNumber);
throw PropertyAccessException.PropIsNotReadableException(PropertyName());
}
set
{
if(_context.IsPropWritable(_contextPropertyNumber))
_context.SetPropValue(_contextPropertyNumber, value);
else
throw PropertyAccessException.PropIsNotWritableException(PropertyName());
}
}
private string PropertyName() => _context.GetPropertyInfo(_contextPropertyNumber).Name;
}
}