private void SetTunneledVariable(object valueToSetTo, bool attachAndUnattach, object untranslated, string variableName,
object objectToSetOn, IElement container, NamedObjectSave nos)
{
if (objectToSetOn is ElementRuntime)
{
ElementRuntime asElementRuntime = objectToSetOn as ElementRuntime;
CustomVariable containedVariable = asElementRuntime.GetCustomVariable(variableName);
bool wasFound = false;
if (containedVariable != null) //VIC!!!! This case needs to be handled. The contained variable might be null if I'm tunneling to an Entity which doesn't expose said variable.
{
// This method will internally translate the value. This could cause double-translation
// so we want to just pass the raw here:
asElementRuntime.SetCustomVariable(containedVariable, container, untranslated, attachAndUnattach);
wasFound = true;
}
if (!wasFound && valueToSetTo is string)
{
// don't want to translate states
StateSave stateSave = asElementRuntime.GetStateRecursively((string)untranslated);
if (stateSave != null)
{
wasFound = true;
asElementRuntime.SetState(stateSave, false, container);
}
}
else if (!wasFound && valueToSetTo is StateSave)
{
StateSave stateSave = valueToSetTo as StateSave;
if (stateSave != null)
{
wasFound = true;
asElementRuntime.SetState(stateSave, false, container);
}
}
}
else if (variableName == "SourceFile")
{
// We're setting the source, which means it's setting the file new:
if (ShouldElementRuntimeBeCreatedForNos(nos, container))
{
Layer layerToPutOn = GetLayerForNos(this.mLayer, nos);
// Setting the source file should make the NOS pretend like it's loading from a particular
// file, so let's set the NOS variables temporarily.
// I'm not 100% certain this is the best approach, but so much code is integrated into the NOS
// and I don't want to duplicate that logic, or tear out the variables because it would make the
// methods have really long parameter lists (which they already do)
string className = nos.SourceClassType;
var oldSourceType = nos.SourceType;
var oldSourceFile = nos.SourceFile;
var oldSourceName = nos.SourceName;
string foundName = null;
// We need to convert the variable name to the source RFS
foreach (var rfs in container.ReferencedFiles)
{
if (rfs.GetInstanceName() == (string)valueToSetTo)
{
foundName = rfs.Name;
}
}
if (!string.IsNullOrEmpty(foundName))
{
nos.SourceType = SourceType.File;
nos.SourceFile = foundName;
nos.SourceName = "Entire File (" + className + ")";
LoadFileObject(nos, container, layerToPutOn,
this.mContainedElements);
nos.SourceType = oldSourceType;
nos.SourceFile = oldSourceFile;
nos.SourceName = oldSourceName;
}
//LoadFileObject(nos, container, layerToPutOn,
// mContainedElements, false);
}
}
}